简体   繁体   English

NLog:从 nlog.config 切换到程序化配置

[英]NLog: Switching from nlog.config to programmatic configuration

Previously my ASP .NET MVC5 application had error logging working great with the use of a nlog.config file.以前,我的 ASP .NET MVC5 应用程序在使用 nlog.config 文件时可以很好地记录错误日志。 Now I want to move away from the config file approach;现在我想摆脱配置文件的方法; I want to configure Nlog programmatically and remove the need for the config file.我想以编程方式配置 Nlog 并消除对配置文件的需要。 The examples are good at showing me how to set it up programmatically, but I'm confused as to how to actually invoke the configuration class and actually implementing it.这些示例很好地向我展示了如何以编程方式设置它,但我对如何实际调用配置类并实际实现它感到困惑。 Heres what I have:这是我所拥有的:

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);      
    }

Does anyone know what this isn't working?有谁知道这不起作用? Heres the documentation I based this off of: https://github.com/nlog/NLog/wiki/Configuration-API这是我基于此的文档: https : //github.com/nlog/NLog/wiki/Configuration-API

If it helps, here's what I had back when I used the nlog.config file and it worked fine:如果有帮助,以下是我使用 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);     
    }

Any help much appreciated!非常感谢任何帮助! Thankyou!谢谢!

Do you run your application with enough rights to write a log file in the root of your C-drive?您是否以足够的权限运行您的应用程序以在 C 驱动器的根目录中写入日志文件? Try it with ${basedir}/nLogFile.txt and just see if this works.用 ${basedir}/nLogFile.txt 试试看,看看它是否有效。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM