标签: nlog

当 NLog 配置为从代码登录时,它不会登录数据库

我有以下控制台应用程序,它使用编程配置使用 NLog 实现日志记录。目前,它尚未登录数据库,并且在运行时也不会抛出任何异常。有人可以让我知道其背后的原因吗?

using NLog;
using NLog.Config;
using NLog.Targets;
using System;

namespace Test.Logger
{
    class Program
    {
        static void Main(string[] args)
        {
            var logConfig = new LoggingConfiguration();


            //dbtarget 2
            var dbTarget2 = new DatabaseTarget();
            dbTarget2.ConnectionString = "SomeConnectionString";
            dbTarget2.CommandText = @"INSERT INTO [dbo].[LogAppDetail]
                                                   ([LogAppIS]
                                                   ,[LogAppId]
                                                   ,[DN]
                                                   ,[DV])
                                             VALUES
                                                   (@LogAppIS
                                                   ,@LogAppId
                                                   ,@DN
                                                   ,@DV)";

            dbTarget2.Parameters.Add(new DatabaseParameterInfo("@LogAppIS", new NLog.Layouts.SimpleLayout("${event-properties:LogAppIS}")));
            dbTarget2.Parameters.Add(new DatabaseParameterInfo("@LogAppId", new NLog.Layouts.SimpleLayout("${event-properties:LogAppId}")));
            dbTarget2.Parameters.Add(new DatabaseParameterInfo("@DN", new NLog.Layouts.SimpleLayout("${event-properties:DN}")));
            dbTarget2.Parameters.Add(new DatabaseParameterInfo("@DV", new NLog.Layouts.SimpleLayout("${event-properties:DV}")));

            logConfig.AddTarget("dbTarget2", dbTarget2);
            // add rules
            var rule2 = new LoggingRule("LogAppDetail", LogLevel.Trace, dbTarget2);
            logConfig.LoggingRules.Add(rule2); …
Run Code Online (Sandbox Code Playgroud)

c# nlog

2
推荐指数
1
解决办法
3253
查看次数

NLog 在 C# 中获取日志消息

我最近开始使用 NLog,我想在将日志保存到文件中之前在我的应用程序中显示日志。

我怎样才能得到以下内容?

string message = logger.getLog() <---- This is what I need
Run Code Online (Sandbox Code Playgroud)

c# nlog

2
推荐指数
1
解决办法
4540
查看次数

如何使用 Sustainsys.Saml2 设置 NLog

我有一个 ASP.Net Web Forms 应用程序,我刚刚在其中集成了Sustainsys.Saml2库。

我从未使用过任何类型的日志记录机制,并且我正在尝试找出如何为其故障排除ILoggerAdapter页面上所述的库添加或创建日志记录机制。

我决定使用 NLog(请随意推荐一个不同的),要么我不太理解这一点,要么没有使用正确的关键字来搜索我需要/想要的东西,或者它们的数量不是很多有关它的文档。

目前,我正在使用的HttpModule版本Sustainsys.Saml2。根据要求提供的任何其他信息。

任何帮助都会很棒。

目前,我正在Sustainsys.Saml2通过 web.config 和 global.asax 文件配置该库。这是我的 global.asax 调用的类:

public class Saml2Config {
    private static bool _alreadyInitialized;
    private static readonly object Lock = new object();

    public static void Initialize() {
        if (_alreadyInitialized) {
            return;
        }

        lock (Lock) {
            if (_alreadyInitialized) {
                return;
            }

            var domain = PageHelper.GetDomainURL(true);

            Sustainsys.Saml2.Configuration.Options.FromConfiguration.SPOptions.EntityId.Id = $"{domain}/federation/Saml2";
            Sustainsys.Saml2.Configuration.Options.FromConfiguration.SPOptions.ModulePath = "/federation/Saml2";
            Sustainsys.Saml2.Configuration.Options.FromConfiguration.SPOptions.ReturnUrl = new Uri($"{domain}/mybarry");
            Sustainsys.Saml2.Configuration.Options.FromConfiguration.SPOptions.PublicOrigin = new Uri($"{domain}"); …
Run Code Online (Sandbox Code Playgroud)

c# nlog sustainsys-saml2

2
推荐指数
1
解决办法
1203
查看次数

AspNet Core 5.0并注册自定义NLog LayoutRenderer

我在 ASP Net Core 5.0 应用程序中使用 NLog.Web.AspNetCore 4.10.0。我创建了一个简单的自定义 ResultLayoutRenderer (为简单起见缩短):

[LayoutRenderer("result-payload")]
public class ResultLayoutRenderer : AspNetLayoutRendererBase
{
    protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
    {
        var httpRequest = HttpContextAccessor?.HttpContext?.Request;
        var result = httpRequest.Headers["Result"];
        builder.Append(result);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的 NLog 配置如下:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true">

    <!-- enable asp.net core layout renderers -->
    <extensions>
        <add assembly="NLog.Web.AspNetCore"/>
    </extensions>
    
    <targets>
        <target xsi:type="Null" name="blackhole" />
        <target name="database" xsi:type="Database"
            dbProvider="Npgsql.NpgsqlConnection, Npgsql"
            dbHost="${configsetting:name=NlogConnection.DbHost}"
            dbDatabase="${configsetting:name=NlogConnection.Database}"
            dbUserName="${configsetting:name=NlogConnection.User}"
            dbPassword="${configsetting:name=NlogConnection.Password}"">
            
            <commandText>
                INSERT INTO logs (Application, Logged, Level, Message, …
Run Code Online (Sandbox Code Playgroud)

c# nlog asp.net-core-5.0

2
推荐指数
1
解决办法
2562
查看次数

NHibernate与NLog或Log4Net或其他什么?

我想在我的项目中使用NHibernate,默认情况下它带有log4net.

在我看来,对于Nlog来说它是一个更明智的选择,因为它仍然会得到更新,另一方面,log4net已经完全过时了.

有没有最好的选择,因为我真的不相信在我的应用程序中使用过时的技术.另一方面,它是在nhibernate包中交付的那个......

欢迎提出建议!

*用'nolonger-updated'替换过时的

asp.net nhibernate log4net nlog

1
推荐指数
2
解决办法
880
查看次数

使用控制台查看NLog信息

这可能是一个非常愚蠢的问题,但我们走了.我的Web应用程序中有以下NLog.config,但是对话框如何/何时打开并显示信息,或者我是否需要启动它并导航到特定目录或其他内容?

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.netfx35.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target xsi:type="ColoredConsole"
            name="Debug"
            layout="${message} ${exception}">
      <highlight-row backgroundColor="Black"
                     foregroundColor="DarkCyan"
                     condition="true"/>
    </target>
  </targets>
  <rules>
    <logger name="Raven.Client.*" writeTo="Debug"/>
  </rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)

logging nlog

1
推荐指数
1
解决办法
5587
查看次数

使用ServiceStack.Logging.NLog而不是直接NLog调用有什么缺点?

我喜欢NLOG由于描述的多种原因  https://robertmccarter.com/switching-to-nlog  和

log4net与Nlog

但是我考虑   https://github.com/ServiceStack/ServiceStack  和 https://nuget.org/packages/ServiceStack.Logging.NLog以保持通用接口和将来切换到其他提供商的能力.

是否有人知道使用ServiceStack.Logging.NLog而不是直接NLog调用的任何缺点/限制?

它会导致任何重要的性能下降吗?

直接调用NLog记录器时是否可以使用任何功能,但ServiceStack不支持?

logging nlog servicestack

1
推荐指数
1
解决办法
985
查看次数

是否有可能动态告诉NLog要登录的目标?

我想AppDomain.FirstChanceException在我的WPF应用程序中实现,以便可选地记录发生,处理或不处理的每个异常.我不希望将这些例外记录到我为NLog配置的目标.是否有可能,在调用时Logger.Error(或任何Logger方法)使NLog只记录到一个特定的目标?

wpf logging nlog unhandled-exception first-chance-exception

1
推荐指数
1
解决办法
1311
查看次数

启动服务上的NLog异常

我正在做一个服务,现在是空的,只使用NLog,但是当我启动服务时,我得到以下错误;

---------------------------
Services
---------------------------
Windows could not start the JobService service on Local Computer.

Error 1053: The service did not respond to the start or control request in a timely fashion.

---------------------------
OK   
---------------------------
Run Code Online (Sandbox Code Playgroud)

这是事件查看器中的堆栈跟踪:

Application: service.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Configuration.ConfigurationErrorsException
Stack:
   at System.Configuration.ClientConfigurationSystem.EnsureInit(System.String)
   at System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(System.String)
   at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(System.String)
   at System.Configuration.ConfigurationManager.GetSection(System.String)
   at NLog.Config.XmlLoggingConfiguration.get_AppConfig()
   at NLog.LogFactory.get_Configuration()
   at NLog.LogFactory.GetLogger(LoggerCacheKey)
   at NLog.LogFactory.GetLogger(System.String)
   at NLog.LogManager.GetLogger(System.String)
   at JobsService.Service1..ctor()
Run Code Online (Sandbox Code Playgroud)

NLog.config:

<?xml version="1.0" encoding="utf-8" ?>
<nlog …
Run Code Online (Sandbox Code Playgroud)

c# windows-services nlog

1
推荐指数
1
解决办法
775
查看次数

由于'Logger.Logger()的保护级别,我无法创建新的NLog Logger

我目前正在使用C#开发一个新的Prism项目,并希望在我的Bootstrapper类中创建一个NLog Logger.不幸的是,无论我做什么都拒绝建立,告诉我......

由于其保护级别,"Logger.Logger()"无法访问.

我的代码片段列在下面......

namespace PrismTemplate
{

    public class Bootstrapper : UnityBootstrapper
    {
        #region Method Overrides

        protected override ILoggerFacade CreateLogger()
        {
            //ILoggerFacade logger = new NLogLogger();
            ILoggerFacade logger = new Logger();
            logger.Log("Prism Template Logger was Created.", Category.Info, Priority.None);
            return logger;

            //return new Logger();
            //return null;
        }

        protected override DependencyObject CreateShell()
        {
            return new ShellWindow();
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();

            ///Assign the shell which is a window to the apps main window, and show it

            App.Current.MainWindow = (Window)this.Shell;
            App.Current.MainWindow.Show(); …
Run Code Online (Sandbox Code Playgroud)

c# wpf logging prism nlog

1
推荐指数
1
解决办法
543
查看次数