【问题标题】:NLog: Switching from nlog.config to programmatic configurationNLog:从 nlog.config 切换到编程配置
【发布时间】:2014-07-27 01:06:28
【问题描述】:

以前,我的 ASP .NET MVC5 应用程序在使用 nlog.config 文件时可以很好地记录错误。现在我想摆脱配置文件的方法;我想以编程方式配置 Nlog 并删除对配置文件的需要。这些示例很好地向我展示了如何以编程方式设置它,但我对如何实际调用配置类并实际实现它感到困惑。这是我所拥有的:

protected void Application_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();
        NALSPrincipal userInfo = (NALSPrincipal)Context.User;

        // Step 1. Create configuration object 
        var config = new LoggingConfiguration();

        // Step 2. Create targets and add them to the configuration 
        var fileTarget = new FileTarget();
        config.AddTarget("file", fileTarget);

        // Step 3. Set target properties 
        fileTarget.FileName = "C:/nlogFile.txt";
        fileTarget.Layout = "Exception Type: ${exception:format=Type}${newline}
                             Target Site:  ${event-context:TargetSite}${newline}
                             Message: ${message}";

        // Step 4. Define rules
        var rule2 = new LoggingRule("*", LogLevel.Trace, fileTarget);
        config.LoggingRules.Add(rule2);

        // Step 5. Activate the configuration
        LogManager.Configuration = config;

        Logger logger = LogManager.GetCurrentClassLogger();
        LogEventInfo eventInfo = new LogEventInfo(LogLevel.Trace, "", logger.Name);
        eventInfo.Properties["TargetSite"] = ex.TargetSite;
        eventInfo.Exception = ex;
        logger.Log(eventInfo);      
    }

有谁知道这不起作用吗?这是我基于此的文档: https://github.com/nlog/NLog/wiki/Configuration-API

如果有帮助,这是我在使用 nlog.config 文件时得到的结果,它运行良好:

protected void Application_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();
        NALSPrincipal userInfo = (NALSPrincipal)Context.User;
        Logger logger = LogManager.GetCurrentClassLogger();
        LogEventInfo eventInfo = new LogEventInfo(LogLevel.Trace, "", logger.Name);
        eventInfo.Properties["CUID"] = userInfo.userDetails.ContactID;
        eventInfo.Properties["Username"] = Context.User.Identity.Name;
        eventInfo.Properties["TargetSite"] = ex.TargetSite;
        eventInfo.Exception = ex;
        logger.Log(eventInfo);     
    }

非常感谢任何帮助!谢谢!

【问题讨论】:

    标签: c# asp.net-mvc logging asp.net-mvc-5 nlog


    【解决方案1】:
    private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
    
    var configuration = new NLog.Config.LoggingConfiguration();
    var logfile = new NLog.Targets.FileTarget("logfile") { FileName = "logfile.txt" };
    var logconsole = new NLog.Targets.ConsoleTarget("logconsole");
    
    configuration.AddRule(NLog.LogLevel.Trace, NLog.LogLevel.Fatal, logfile);
    configuration.AddRule(NLog.LogLevel.Trace, NLog.LogLevel.Fatal, logconsole);
    
    NLog.LogManager.Configuration = configuration;
    

    【讨论】:

      【解决方案2】:

      您是否以足够的权限运行您的应用程序,以便在 C 驱动器的根目录中写入日志文件?用 ${basedir}/nLogFile.txt 试试看是否可行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-21
        • 2016-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多