C# 使用log4net写日志,可保存到文件或Sql Server

一、 使用方法

  1. 在AssemblyInfo.cs文件的末尾加上下面一行代码:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
  1. 将log4net.config源文件复制到项目根目录下
  2. 根据需要 修改 日志记录级别
  3. 在项目的根目录下建立Logger文件夹
  4. using log4net;
  5. private static readonly ILog logger = LogManager.GetLogger(typeof(FrmMain));
  6. logger.Debug(“日志”);

二、log4net.config源文件

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    </configSections>
    <log4net debug="false">
        <root>
          <!--() OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL () -->
          <level value="ALL"/>
          <appender-ref ref="RollingLogFileAppender"/>
        </root>
        <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
            <!-- 日志文件路径 -->
            <param name="File" value="Logger\"/>
            <!--是否是向文件中追加日志-->
            <param name="AppendToFile" value="true"/>
            <!--记录日志写入文件时,不锁定文本文件-->
            <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
            <!--log保留天数-->
            <param name="MaxSizeRollBackups" value="365"/>
            <!--日志文件名是否是固定不变的-->
            <param name="StaticLogFileName" value="false"/>
            <!--日志文件名格式为:2008-08-31.log-->
            <param name="DatePattern" value="yyyy-MM'.log'"/>
            <!--日志根据日期滚动-->
            <param name="RollingStyle" value="Date"/>
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="[%date] %level %l - %m%n"/>
            </layout>
            <filter type="log4net.Filter.LevelRangeFilter">
                <param name="LevelMin" value="Info"/>
                <param name="LevelMax" value="Fatal"/>
            </filter>
        </appender>     
        <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
            <bufferSize value="0"/>
            <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
            <connectionString value="Data Source=127.0.0.1,1433;Initial Catalog=数据库名;user id=用户名;password=密码"/>
            <commandText value="INSERT INTO TLog ([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"/>
                </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>
        </appender>
    </log4net>
</configuration>

三、Sql Server脚本

CREATE TABLE [dbo].[TLog] (
  [ID] int  IDENTITY(1,1) NOT NULL,
  [Date] datetime DEFAULT NULL NOT NULL,
  [Thread] varchar(255) COLLATE Chinese_PRC_CI_AS  NOT NULL,
  [Level] varchar(50) COLLATE Chinese_PRC_CI_AS  NOT NULL,
  [Logger] varchar(255) COLLATE Chinese_PRC_CI_AS  NOT NULL,
  [Message] varchar(4000) COLLATE Chinese_PRC_CI_AS  NOT NULL,
  [Exception] varchar(2000) COLLATE Chinese_PRC_CI_AS  NULL
)

你可能感兴趣的:(日志记录,c#,visualstudio)