log4net 自定义的字段

log4net.config 文件,有些字段自己定义,这里我用userName

<?xml version="1.0"?>
<configuration>
  <!--Log4Net config section-->
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>
  <log4net>
    <root>
      <!--OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL-->
      <level value="ALL"/>
      <appender-ref ref="AdoNetAppender_SqlServer"/>
      <appender-ref ref="RollingLogFileAppender"/>
    </root>

    
    <appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
      <!--<filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="Debug" />
        <levelMax value="Info" />
      </filter>-->
      <bufferSize value="0" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
      <connectionString value="data source=10.209.32.21;initial catalog=APPDB;User ID=APPDB;Password=APPDB@2020"/>
      <!--<connectionString value="server=192.168.1.39;database=sunwin_t;user id=sa;password=sunwinbus;Connect Timeout=15;"/>-->
      <commandText value="INSERT INTO ApsLog([Date],[Level],[Logger],[Message],[UserName]) VALUES (@log_date, @log_level, @logger, @message, @username)" />
      <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </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="2000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@username" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{UserName}" />
        </layout>
      </parameter>      
    </appender>
  </log4net>
  <!--Log4Net config section end-->
</configuration>

Global.asax.cs 中加入注册信息

protected void Application_Start()
        {
            

            log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/log4net.config")));

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

 

public class LogHelper
    {
        public static readonly ILog log = LogManager.GetLogger("RollingLogFileAppender");
        public static void Debug(string message)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            if (log.IsDebugEnabled)
            {

                //log4net.LogicalThreadContext.Properties["CustomColumn"] = "Custom value";
                log.Debug(message);
            }
            log = null;
        }
public static void Info(string message,string username)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            if (log.IsInfoEnabled)
            {
                log4net.LogicalThreadContext.Properties["UserName"] = username; // 这句就是自定义字段添加方法
                log.Info(message);
            }
            log = null;
        }

        
    }

 

posted @ 2020-04-03 08:40  sj2016  阅读(321)  评论(0编辑  收藏  举报