4
以前我的ASP .NET MVC5應用程序錯誤日誌記錄工作與使用nlog.config文件很好。現在我想擺脫配置文件的方式;我想以編程方式配置Nlog並刪除配置文件的需要。這些示例很好地向我展示瞭如何以編程方式進行設置,但我很困惑如何實際調用配置類並實際執行它。繼承人我有什麼:NLog:從nlog.config切換到編程配置
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);
}
任何幫助,非常感謝!謝謝!
就是這樣!謝謝 – stackPusher