日志框架
1、NLog
<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"> <!--异步写日志--> <targets async="true"> <!--记录日志到控制台--> <target xsi:type="ColoredConsole" name="consoleLog" encoding="UTF-8" layout="${date:format=yyyy-MM-dd HH\:mm\:ss}|${level:uppercase=true}|${logger}${newline}${message}${newline}" /> <!--记录日志到文件--> <target xsi:type="File" name="fileLog" encoding="UTF-8" fileName="${basedir}/LogFile/${shortdate}.log" layout="${date:format=yyyy-MM-dd HH\:mm\:ss}|${level:uppercase=true}|${logger}${newline}${message}${newline}${newline}" /> <!--记录日志到SqlServer数据库--> <target xsi:type="Database" name="dbLog" encoding="UTF-8"> <connectionString>Data Source=localhost;Initial Catalog=Demo;Persist Security Info=True;User ID=sa;Password=123456789;</connectionString> <dbProvider>System.Data.SqlClient</dbProvider> <commandText> INSERT INTO dbo.Log (Logged,Level,Message,Username,ServerName,Logger) VALUES (@Logged,@Level,@Message,@Username,@ServerName,@Logger) </commandText> <parameter name="@logged" layout="${date}" /> <parameter name="@level" layout="${level}" /> <parameter name="@message" layout="${message}" /> <parameter name="@username" layout="${identity}" /> <parameter name="@serverName" layout="${machinename}" /> <parameter name="@logger" layout="${logger}" /> </target> <!--发送邮件日志--> <target xsi:type="Mail" name ="mailLog" encoding="UTF-8" smtpServer="smtp.gmail.com" smtpPort="587" smtpAuthentication="Basic" smtpUsername="邮箱用户名" smtpPassword="邮箱密码" enableSsl="true" addNewLines="true" from="发件人" to="收件人(逗号分隔)" cc="抄送" html="true" encoding="UTF-8" subject="邮件主题" header="************************************************************************" body="${newline} 时间:${date:format=yyyy-MM-dd HH\:mm\:ss} ${newline}${newline} 级别:${level:uppercase=true} ${newline}${newline} Logger:${logger} ${newline}${newline} 详情:${message} ${newline}${newline}" footer="************************************************************************" /> </targets> <rules> <logger name="*" minLevel="Debug" maxlevel="Fatal" writeTo="consoleLog" /> <!--<logger name="*" minLevel="Debug" maxlevel="Fatal" writeTo="fileLog" /> <logger name="*" minLevel="Debug" maxlevel="Fatal" writeTo="dbLog" /> <logger name="*" minLevel="Debug" maxlevel="Fatal" writeTo="mailLog" />--> </rules> </nlog>
2、Log4Net
1 <?xml version="1.0"?> 2 <configuration> 3 <configSections> 4 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> 5 </configSections> 6 <!--日志记录组建配置--> 7 <log4net> 8 <!-- Console部分log输出格式的设定 --> 9 <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> 10 <layout type="log4net.Layout.PatternLayout"> 11 <conversionPattern value="[%-5p]|%d|%logger|[%ndc] %n%m%n" /> 12 </layout> 13 </appender> 14 <!-- 日志文件部分log输出格式的设定 --> 15 <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> 16 <file value="Logs\" /> 17 <!--多线程时采用最小锁定--> 18 <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> 19 <!--是否追加到文件,默认为true,通常无需设置--> 20 <appendToFile value="true" /> 21 <rollingStyle value="Date" /> 22 <!--日期的格式,每天换一个文件记录,如不设置则永远只记录一天的日志,需设置--> 23 <datePattern value="yyyyMMdd'.txt'" /> 24 <staticLogFileName value="false" /> 25 <layout type="log4net.Layout.PatternLayout"> 26 <header value="" /> 27 <!--[%-5p]:日志级别,格式化为5个宽度 [%d]日志时间 [%logger]:日志记录者 [%n]:换行 [%m]:日志内容--> 28 <ConversionPattern value="[%-5p]|%d|%logger|[%ndc] %n%m%n%n" /> 29 </layout> 30 </appender> 31 32 <!-- Setup the root category, add the appenders and set the default level --> 33 <root> 34 <!--控制级别,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF--> 35 <!--比如定义级别为INFO,则INFO级别向下的级别,比如DEBUG日志将不会被记录--> 36 <!--如果没有定义LEVEL的值,则缺省为DEBUG--> 37 <level value="DEBUG" /> 38 <!--<appender-ref ref="ConsoleAppender" />--> 39 <appender-ref ref="RollingLogFileAppender" /> 40 </root> 41 </log4net> 42 </configuration>