1.通过nuget安装Log4net和mysql.data
?
2.在项目目录下新建Log4net.config?
3.log4net.config内容如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<!-- 输出模式定义 -->
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<!--日志路径: 兼容相对路径和绝对路径,此语句等价于<File value="Logs\"/> -->
<param name= "File" value= "Logs\"/>
<!--是否是向文件中追加日志-->
<param name= "AppendToFile" value= "true"/>
<!--保留天数-->
<param name= "MaxSizeRollBackups" value= "-1"/>
<param name="MaximumFileSize" value="3MB" />
<!--<param name="MaxTotalLogFiles" value="3MB" />-->
<!--日志文件名是否固定不变的-->
<param name= "StaticLogFileName" value= "false"/>
<!--日志文件名格式为:2017-01-12_Info.log-->
<param name= "DatePattern" value= "yyyy-MM-dd/yyyy-MM-dd HH_'Info.log'"/>
<!--日志根据日期滚动-->
<param name= "RollingStyle" value= "Date"/>
<!-- 写入记录文件时的格式%p应该指模式如WARN,%m应该指Message值,%n应该是换行的意思。接着输出Exception -->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss.fff} %-8p %m%n" />
</layout>
<!-- 此模式输出的级别范围 -->
<!-- FATAL > ERROR > WARN > INFO > DEBUG -->
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="INFO" />
</filter>
</appender>
<appender name="WarnAppender" type="log4net.Appender.RollingFileAppender">
<File value= "Logs\"/>
<param name= "AppendToFile" value= "true"/>
<param name= "MaxSizeRollBackups" value= "10"/>
<!-- 能保存的文件最大值,假设某次输出后该文件如mm_warn.log的大小将会超过最大值3KB,则此次输出仍有效,但是下次输出则会将该文件重命名为mm_warn.log.1,且生成一个新的mm_warn.log文件以供记录输出内容。 -->
<param name="MaximumFileSize" value="3MB" />
<!--<param name="MaxTotalLogFiles" value="3MB" />-->
<param name= "StaticLogFileName" value= "false"/>
<param name= "DatePattern" value= "yyyy-MM-dd/yyyy-MM-dd HH_'Warn.log'"/>
<param name= "RollingStyle" value= "Composite"/>
<!-- 写入记录文件时的格式 -->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss.fff} %-8p %m%n" />
</layout>
<!-- 此模式输出的级别范围 -->
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<levelMax value="WARN" />
</filter>
</appender>
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<File value= "Logs\"/>
<param name= "AppendToFile" value= "true"/>
<param name= "MaxSizeRollBackups" value= "10"/>
<!-- 能保存的文件最大值,假设某次输出后该文件如mm_warn.log的大小将会超过最大值3KB,则此次输出仍有效,但是下次输出则会将该文件重命名为mm_warn.log.1,且生成一个新的mm_warn.log文件以供记录输出内容。 -->
<param name="MaximumFileSize" value="3MB" />
<!--<param name="MaxTotalLogFiles" value="3MB" />-->
<param name= "StaticLogFileName" value= "false"/>
<param name= "DatePattern" value= "yyyy-MM-dd/yyyy-MM-dd HH_'Error.log'"/>
<param name= "RollingStyle" value= "Composite"/>
<!-- 写入记录文件时的格式 -->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss.fff} %-8p %m%n" />
</layout>
<!-- 此模式输出的级别范围 -->
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="ERROR" />
</filter>
</appender>
<appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
<File value= "Logs\"/>
<param name= "AppendToFile" value= "true"/>
<param name= "MaxSizeRollBackups" value= "10"/>
<!-- 能保存的文件最大值,假设某次输出后该文件如mm_warn.log的大小将会超过最大值3KB,则此次输出仍有效,但是下次输出则会将该文件重命名为mm_warn.log.1,且生成一个新的mm_warn.log文件以供记录输出内容。 -->
<param name="MaximumFileSize" value="3MB" />
<!--<param name="MaxTotalLogFiles" value="3MB" />-->
<param name= "StaticLogFileName" value= "false"/>
<param name= "DatePattern" value= "yyyy-MM-dd/yyyy-MM-dd HH_'Debug.log'"/>
<param name= "RollingStyle" value= "Composite"/>
<!-- 写入记录文件时的格式 -->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss.fff} %-8p %m%n" />
</layout>
<!-- 此模式输出的级别范围 -->
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="DEBUG" />
</filter>
</appender>
<appender name="FatalAppender" type="log4net.Appender.RollingFileAppender">
<File value= "Logs\"/>
<param name= "AppendToFile" value= "true"/>
<param name= "MaxSizeRollBackups" value= "10"/>
<!-- 能保存的文件最大值,假设某次输出后该文件如mm_warn.log的大小将会超过最大值3KB,则此次输出仍有效,但是下次输出则会将该文件重命名为mm_warn.log.1,且生成一个新的mm_warn.log文件以供记录输出内容。 -->
<param name="MaximumFileSize" value="6MB" />
<!--<param name="MaxTotalLogFiles" value="3MB" />-->
<param name= "StaticLogFileName" value= "false"/>
<param name= "DatePattern" value= "yyyy-MM-dd/yyyy-MM-dd HH_'Fatal.log'"/>
<param name= "RollingStyle" value= "Composite"/>
<!-- 写入记录文件时的格式 -->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss.fff} %-8p %m%n" />
</layout>
<!-- 此模式输出的级别范围 -->
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="FATAL" />
<levelMax value="FATAL" />
</filter>
</appender>
<appender name="AdoNetAppender12" type="log4net.Appender.AdoNetAppender">
<bufferSize value="0" />
<connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.0.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
<connectionString value="server=127.0.0.1;port=3306;user=root;password=123456; database=smartlabel;" providerName="MySql.Data.MySqlClient;" />
<commandText value="INSERT INTO Log (Date,Thread,Level,Logger,Message,Exception) VALUES (@log_date,@thread,@log_level,@logger, @message, @exception);" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
<!--%level-->
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
<!--<logger name="logadonet">
<level value="ALL"/>
<appender-ref ref="AdoNetAppender"/>
</logger>-->
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="ALL" />
<!--<appender-ref ref="ConsoleAppender" />-->
<appender-ref ref="DebugAppender" />
<appender-ref ref="InfoAppender" />
<appender-ref ref="WarnAppender" />
<appender-ref ref="ErrorAppender" />
<appender-ref ref="FatalAppender" />
<!--<appender-ref ref="RollingLogFileAppender" />-->
<appender-ref ref="AdoNetAppender12" />
</root>
<!--<logger name="*">
<level value="ALL" />
<appender-ref ref="AdoNetAppender11" />
</logger>-->
</log4net>
</configuration>
4.在app.config?中加入如下内容 ,appsettings中打开log4net调试功能
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<add key="log4net.Internal.Debug" value="true"/>
5.在assemblyinfo.cs中加入如下内容
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "ConfigFile/log4net.config", ConfigFileExtension = "config", Watch = true)]
6.在app.xaml.cs中加入
public static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
?7.在代码中使用
App.Log.Error("日志1");
App.Log.Info("日志1");
App.Log.Warn("日志1");
8.保存到数据效果
?9.数据库格式
?
|