Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NLog:从NLog.config切换到编程配置_C#_Asp.net Mvc_Logging_Asp.net Mvc 5_Nlog - Fatal编程技术网

C# NLog:从NLog.config切换到编程配置

C# NLog:从NLog.config切换到编程配置,c#,asp.net-mvc,logging,asp.net-mvc-5,nlog,C#,Asp.net Mvc,Logging,Asp.net Mvc 5,Nlog,以前,我的ASP.NET MVC5应用程序在使用nlog.config文件时出现了错误日志记录。现在我想离开配置文件方法;我希望以编程方式配置Nlog,并消除对配置文件的需要。这些示例很好地向我展示了如何以编程方式设置它,但我对如何实际调用配置类并实际实现它感到困惑。以下是我所拥有的: protected void Application_Error(object sender, EventArgs e) { Exception ex = Server.GetLastEr

以前,我的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);      
    }
有人知道这有什么问题吗?以下是我根据以下内容编制的文件:

如果有帮助的话,下面是我使用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-drive的根目录中写入日志文件?使用${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;