NLog是什么?
NLog是一个简单灵活的.NET日志记录类库,可以免费使用的开源代码。
NLog日志输出什么类型?
输出文本文件,如TXT文件
输出控制台
输出EMAIL邮件
输出数据库
输出Windows系统日志等等
如何使用NLog组件?
1)下载NLog组件
通过VS2015的NuGet下载组件,见图
下载NLog和NLog.Config即可。
2)配置NLog.Config文件
关于NLog配置参数,请参考NLog官网:http://nlog-project.org/
源代码托管地址:https://github.com/nlog/NLog/wiki/Configuration-file
<target name="database" xsi:type="Database" keepConnection="false" useTransactions="true"
dbProvider="Oracle.ManagedDataAccess.Client"
connectionStringName="OraConnstr" <!—名称为APP.CONFIG中的connectionStrings 中的NAME名称-->
<parameter name="APPNAME" layout="${event-context:item=APPNAME}" />
<parameter name="MODULENAME" layout="${event-context:item=MODULENAME}" />
<parameter name="PROCNAME" layout="${event-context:item=PROCNAME}" />
<parameter name="OPERATIONTYPE" layout="${event-context:item=OPERATIONTYPE}" />
<parameter name="LOGGER" layout="${event-context:item=LOGGER}" />
<parameter name="LOGMESSAGE" layout="${event-context:item=LOGMESSAGE}" />
<parameter name="IP" layout="${event-context:item=IP}" />
<parameter name="TIME_STAMP" layout="${longdate}" />
<parameter name="USERNAME" layout="${event-context:item=USERNAME}" />
<parameter name="LOGLEVEL" layout="${level:uppercase=true}" />
<dbProvider>Oracle.ManagedDataAccess.Client</dbProvider>
<!—写事件日志到 Visual Studio Output-->
<!-- write log message to email-->
<target xsi:type="Mail" name="infoMail"
from="你的发送126邮箱地址 " <!—例如:ssss@126.com-->
to="接收的邮箱地址 " <!—例如:XXX@QQ.com-->
body="${newline}${message}${newline}"
<logger name="*" minlevel="Debug" writeTo="file" />
<!--TRACE,DEBUG,INFO,WARN,ERROR,FATAL-->
<logger name="*" minlevel="Trace" writeTo="debugger" />
<logger name="*" minlevel="Info" writeTo="database"/>
<logger name="*" minlevel="Error" writeTo="infoMail" />
StringBuilder sb = new StringBuilder(STRINGMAX);
WinAPI.GetPrivateProfileString("LOGMANAGER", "DATALOGSOURCE", "", sb, STRINGMAX, CONFIGPATH);
DATALOGSOURCE = sb.ToString();
WinAPI.GetPrivateProfileString("LOGMANAGER", "DATALOGNAME", "", sb, STRINGMAX, CONFIGPATH);
WinAPI.GetPrivateProfileString("LOGMANAGER", "MODE", "", sb, STRINGMAX, CONFIGPATH);
if (!EventLog.SourceExists(DATALOGSOURCE))
EventLog.CreateEventSource(DATALOGSOURCE, DATALOGNAME);
SysLog = new EventLog(DATALOGNAME);
throw new Exception(ex.Message);
SysLog.Source = DATALOGSOURCE;
SysLog.WriteEntry(msg, EventLogEntryType.Error);
WriteDB(msg, LogLevel.Error, APPNAME, MODULENAME, PROCNAME, OPERATIONTYPE, IP, USERNAME, LOGGER);
LOGMESSAGE = LOGMESSAGE.Substring(0, 3000);
LogEventInfo lei = new LogEventInfo();
lei.Properties["ID"] = Guid.NewGuid().ToString("D");
lei.Properties["APPNAME"] = APPNAME;
lei.Properties["MODULENAME"] = MODULENAME;
lei.Properties["PROCNAME"] = PROCNAME;
lei.Properties["OPERATIONTYPE"] = OPERATIONTYPE;
lei.Properties["LOGGER"] = LOGGER;
lei.Properties["LOGMESSAGE"] = LOGMESSAGE;
lei.Properties["TIME_STAMP"] = DateTime.Now;
lei.Properties["USERNAME"] = USERNAME;
lei.Properties["LOGLEVEL"] = Level.Name;
源代码我已上传到百度网盘:链接:https://pan.baidu.com/s/1utnhzcyZJ_7gpnALAsZWDA