查找在您的 .NET 应用程序中设置 Nlog 的分步信息。
第 1 步:- 安装
从 Nuget Manager 安装 NLog 和 NLog.Extended 版本 4.0.0.0。
第 2 步:- WebConfig 配置
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" throwExceptions="false" internalLogFile="D:\NLogErrors\log.txt">
<extensions>
<!-- load NLog.Extended to enable ASP.NET-specific functionality -->
<add assembly="NLog.Extended" />
</extensions>
<!--Define Various Log Targets like files, database or asp.net trace files-->
<targets>
<target name="oracle" xsi:type="Database" keepConnection="false" dbProvider="Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" connectionString="Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = "" )(PORT = 1521))(CONNECT_DATA =(SID = "")));User Id="";Password="";" commandText="INSERT INTO LOGTABLE (LOGMESSAGE,LOGLEVEL,LOGGERNAME,CREATEDDATETIME,SESSIONID,BROWSERDETAIL,REQUESTURL,ERRORMESSAGE,PRODUCERCODE,QUOTENO,CUSTOMER_IP_ADDRESS,SERVER_IP_ADDRESS) values(:LOGMESSAGE,:LOGLEVEL,:LOGGERNAME,:CREATEDDATETIME,:SESSIONID,:BROWSERDETAIL,:REQUESTURL,:ERRORMESSAGE,:PRODUCERCODE,:QUOTENO,:CUSTOMER_IP_ADDRESS,:SERVER_IP_ADDRESS)">
<parameter name="LOGMESSAGE" layout="${message}" />
<parameter name="LOGLEVEL" layout="${level:uppercase=true}" />
<parameter name="LOGGERNAME" layout="${logger}" />
<parameter name="CREATEDDATETIME" layout="${date}" />
<parameter name="SESSIONID" layout="${event-context:item=SessionId}" />
<parameter name="BROWSERDETAIL" layout="${event-context:item=BrowserDetail}" />
<parameter name="REQUESTURL" layout="${event-context:item=RequestUrl}" />
<parameter name="ERRORMESSAGE" layout="${event-context:item=ErrorMessage}" />
<parameter name="PRODUCERCODE" layout="${event-context:item=ProducerCode}" />
<parameter name="QUOTENO" layout="${event-context:item=QuoteNo}" />
<parameter name="CUSTOMER_IP_ADDRESS" layout="${event-context:item=CUSTOMER_IP_ADDRESS}"/>
<parameter name="SERVER_IP_ADDRESS" layout="${event-context:item=SERVER_IP_ADDRESS}"/>
</target>
<target xsi:type="Mail" name="Email" html="true" addNewLines="false" replaceNewlineWithBrTagInHtml="false" subject="Error Log" to="xyz@gmail.com" useSystemNetMailSettings="true" body="${event-context:item=EmailBody}">
</target>
</targets>
<rules>
<logger name="*" minlevel="trace" writeTo="oracle" />
</rules>
</nlog>
第 3 步:- Cs 代码
public static class NLogManager
{
public static ILogger _logger =
NLog.LogManager.GetCurrentClassLogger();
public static void InfoLog(NLogData nLogData)
{
LogEventInfo theEvent = new LogEventInfo(LogLevel.Info, NLogManager._logger.Name, nLogData.Message);
SetLogEventInfo(theEvent, nLogData);
_logger.Log(theEvent);
}
}
private static void SetLogEventInfo(LogEventInfo theEvent, NLogData
nLogData)
{
theEvent.Properties["SessionId"] = nLogData.SessionId;
theEvent.Properties["BrowserDetail"] = nLogData.BrowserDetail;
theEvent.Properties["RequestUrl"] = nLogData.RequestUrl;
theEvent.Properties["ErrorMessage"] = nLogData.ErrorMessage;
theEvent.Properties["EmailBody"] = nLogData.EmailBody;
theEvent.Properties["ProducerCode"] = nLogData.ProducerCode;
theEvent.Properties["QuoteNo"] = nLogData.QuoteNo;
theEvent.Properties["CUSTOMER_IP_ADDRESS"] = nLogData.CustomerIPAddress;
theEvent.Properties["SERVER_IP_ADDRESS"] = nLogData.ServerIPAddress;
}
第 4 步:- 模型实体
public class NLogData
{
public string SessionId { get; set; }
public string Message { get; set; }
public string RequestUrl { get; set; }
public string BrowserDetail { get; set; }
public string Method { get; set; }
public string ErrorMessage { get; set; }
public string EmailBody { get; set; }
public string ProducerCode { get; set; }
public string QuoteNo { get; set; }
public string CustomerIPAddress { get; set; }
public string ServerIPAddress { get; set; }
}
第 5 步:- 在您实施日志记录的位置添加以下行
NLogManager.TraceLog(NLogData nlogData);
-- 创建上述方法接受的正确格式并相应设置值
如果您有任何疑问,请留下评论或发送邮件至 ankitmori14@gmail.com