log4net纯代码配置与c#中的过滤器

发布于 2024-07-21 05:49:02 字数 1324 浏览 9 评论 0原文

我试图纯粹通过代码配置 Log4Net,但是当我使用最小配置进行配置时,来自 NHibernate 和 Fluent 界面的日志消息淹没了我。

所以,我想做的很简单。 告诉 Log4Net 仅显示我的单个类的日志消息。 我玩了一下,但无法弄清楚......

任何人都可以帮忙,我认为下面的代码说明了我的想法:

var filter = new log4net.Filter.LoggerMatchFilter();
filter.LoggerToMatch = typeof(DatabaseDirectory).ToString();
filter.AcceptOnMatch = false;

var x = new log4net.Appender.ConsoleAppender();
x.Layout = new log4net.Layout.SimpleLayout();
x.AddFilter(filter);

log4net.Config.BasicConfigurator.Configure(x);

好的,感谢您的帮助,但这里一定有一些问题。 但我越来越近了。 我尝试了 XML 配置,它有更多的文档。 我设法使用以下 XML 配置实现了预期的结果。 上面的“纯代码”版本肯定存在一些配置错误。

以下 XML 配置提供了“正确”的输出,这与上面代码中的配置不同。 有人看出区别了吗?

<log4net>
<root>
  <level value="DEBUG" />
  <appender-ref ref="ConsoleAppender" />
</root>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
  <filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="Examples.FirstProject.Entities.DatabaseDirectory"/>
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="[%C.%M] %-5p %m%n" />
  </layout>
</appender>

I am trying to configure Log4Net purely by code, but when I did with a minimal configuration, I was flooded by logging messages from NHibernate and the fluent interface.

So, what I am trying to do is simple. Tell Log4Net to show me only log messages of my single class. I toyed around a little bit, but can't figure it out...

Can anyone help, I think the following code illustrates my idea:

var filter = new log4net.Filter.LoggerMatchFilter();
filter.LoggerToMatch = typeof(DatabaseDirectory).ToString();
filter.AcceptOnMatch = false;

var x = new log4net.Appender.ConsoleAppender();
x.Layout = new log4net.Layout.SimpleLayout();
x.AddFilter(filter);

log4net.Config.BasicConfigurator.Configure(x);

Ok, thanks for your help, but there must be some issue here. But I get closer. I tried the XML configuration, which has much more documentation. And I managed to achieve the desired result using the following XML configuration. There must be some misconfiguration in the "pure code" version above.

The following XML configuration provides the "correct" output, which is not the same than the config in code above. Anybody sees the difference?

<log4net>
<root>
  <level value="DEBUG" />
  <appender-ref ref="ConsoleAppender" />
</root>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
  <filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="Examples.FirstProject.Entities.DatabaseDirectory"/>
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="[%C.%M] %-5p %m%n" />
  </layout>
</appender>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尐籹人 2024-07-28 05:49:02

我想通了..有时,只要写下来就会让你大开眼界...

        var filter = new log4net.Filter.LoggerMatchFilter();
        filter.LoggerToMatch = typeof(DatabaseDirectory).ToString();
        filter.AcceptOnMatch = true;

        var filterDeny = new log4net.Filter.DenyAllFilter();


        var x = new log4net.Appender.ConsoleAppender();
        x.Layout = new log4net.Layout.SimpleLayout();
        x.AddFilter(filter);
        x.AddFilter(filterDeny);

        log4net.Config.BasicConfigurator.Configure(x);

看看缺少了什么:-) DenyALL 过滤器!

更多代码示例:

    public static void AllToConsoleSetup()
    {
        var x = new log4net.Appender.ConsoleAppender { Layout = new log4net.Layout.SimpleLayout() };
        log4net.Config.BasicConfigurator.Configure(x);
        SetupDone = true;
    }


    public static void ShowOnlyLogOf(Type t)
    {
        var filter = new log4net.Filter.LoggerMatchFilter {LoggerToMatch = t.ToString(), AcceptOnMatch = true};
        var filterDeny = new log4net.Filter.DenyAllFilter();
        var x = new log4net.Appender.ConsoleAppender {Layout = new log4net.Layout.SimpleLayout()};
        x.AddFilter(filter);
        x.AddFilter(filterDeny);

        log4net.Config.BasicConfigurator.Configure(x);
        SetupDone = true;
    }

确实丑陋但有效(它搞砸了突出显示,不要错过最后几行):

        public static void DefaultSetup()
    {
       // AllToConsoleSetup();
        XmlConfigurator.Configure(XmlSetup());
      // DbConfig();

    }


    private static Stream XmlSetup()
    {
        const string x = @" <log4net>
<root>

  <level value=""ALL"" />
  <appender-ref ref=""AdoNetAppender"">

  </appender-ref>
</root>


<appender name=""AdoNetAppender"" type=""log4net.Appender.AdoNetAppender"">
  <bufferSize value=""1"" />
  <connectionType value=""System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"" />
  <connectionString value=""data source=Christian-PC\SQLEXPRESS;initial catalog=log4net_2;integrated security=false;persist security info=True;User ID=log4net;Password=XXXX"" />
  <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=""655"" />
    <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=""655"" />
    <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>
  <filter type=""log4net.Filter.LoggerMatchFilter"">
    <param name=""LoggerToMatch"" value=""Ruppert"" />
  </filter>
  <filter type=""log4net.Filter.DenyAllFilter"">
  </filter>
</appender>
</log4net>";
        return new MemoryStream(ASCIIEncoding.Default.GetBytes(x));
    }

I figured it out.. Sometimes, just writing it down opens your eyes...

        var filter = new log4net.Filter.LoggerMatchFilter();
        filter.LoggerToMatch = typeof(DatabaseDirectory).ToString();
        filter.AcceptOnMatch = true;

        var filterDeny = new log4net.Filter.DenyAllFilter();


        var x = new log4net.Appender.ConsoleAppender();
        x.Layout = new log4net.Layout.SimpleLayout();
        x.AddFilter(filter);
        x.AddFilter(filterDeny);

        log4net.Config.BasicConfigurator.Configure(x);

See what was missing :-) The denyALL filter!!

Some more code examples:

    public static void AllToConsoleSetup()
    {
        var x = new log4net.Appender.ConsoleAppender { Layout = new log4net.Layout.SimpleLayout() };
        log4net.Config.BasicConfigurator.Configure(x);
        SetupDone = true;
    }


    public static void ShowOnlyLogOf(Type t)
    {
        var filter = new log4net.Filter.LoggerMatchFilter {LoggerToMatch = t.ToString(), AcceptOnMatch = true};
        var filterDeny = new log4net.Filter.DenyAllFilter();
        var x = new log4net.Appender.ConsoleAppender {Layout = new log4net.Layout.SimpleLayout()};
        x.AddFilter(filter);
        x.AddFilter(filterDeny);

        log4net.Config.BasicConfigurator.Configure(x);
        SetupDone = true;
    }

Really UGLY but working (it screws up the highlighting, do not miss the last lines):

        public static void DefaultSetup()
    {
       // AllToConsoleSetup();
        XmlConfigurator.Configure(XmlSetup());
      // DbConfig();

    }


    private static Stream XmlSetup()
    {
        const string x = @" <log4net>
<root>

  <level value=""ALL"" />
  <appender-ref ref=""AdoNetAppender"">

  </appender-ref>
</root>


<appender name=""AdoNetAppender"" type=""log4net.Appender.AdoNetAppender"">
  <bufferSize value=""1"" />
  <connectionType value=""System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"" />
  <connectionString value=""data source=Christian-PC\SQLEXPRESS;initial catalog=log4net_2;integrated security=false;persist security info=True;User ID=log4net;Password=XXXX"" />
  <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=""655"" />
    <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=""655"" />
    <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>
  <filter type=""log4net.Filter.LoggerMatchFilter"">
    <param name=""LoggerToMatch"" value=""Ruppert"" />
  </filter>
  <filter type=""log4net.Filter.DenyAllFilter"">
  </filter>
</appender>
</log4net>";
        return new MemoryStream(ASCIIEncoding.Default.GetBytes(x));
    }
慈悲佛祖 2024-07-28 05:49:02

这是通过代码使用 XMLDocument 加载 xml 来配置 log4net 的另一种方法。 与 Christian 的示例的区别在于,我使用的是 XmlConfigurator.Configure 重载,该重载采用 XmlElement 作为参数。 我还使用单刻度线而不是双引号。 总而言之,我认为它是最干净的。

  string xml =
  @"<log4net>
    <appender name='file1' type='log4net.Appender.RollingFileAppender'>
      <!-- Log file locaation -->
      <param name='File' value='log4net.log'/>
      <param name='AppendToFile' value='true'/>
      <!-- Maximum size of a log file -->
      <maximumFileSize value='2KB'/>
      <!--Maximum number of log file -->
      <maxSizeRollBackups value='8'/>
      <!--Set rolling style of log file -->
      <param name='RollingStyle' value='Composite'/>
      <param name='StaticLogFileName' value='false'/>
      <param name='DatePattern' value='.yyyy-MM-dd.lo\g'/>
      <layout type='log4net.Layout.PatternLayout'>
        <param name='ConversionPattern' value='%d [%t] %-5p  %m%n'/>
      </layout>
    </appender>

    <!-- Appender layout fix to view in console-->
    <appender name='console' type='log4net.Appender.ConsoleAppender'>
      <layout type='log4net.Layout.PatternLayout'>
        <param name='Header' value='[Header]\r\n'/>
        <param name='Footer' value='[Footer]\r\n'/>
        <param name='ConversionPattern' value='%d [%t] %-5p  %m%n'/>
      </layout>
    </appender>

    <appender name='debug' type='log4net.Appender.DebugAppender'>
      <layout type='log4net.Layout.PatternLayout'>
        <param name='ConversionPattern' value='%d [%t] %logger %-5p %m%n'/>
      </layout>
    </appender>
    <root>
      <level value='INFO'/>
      <!--
            Log level priority in descending order:

            FATAL = 1 show  log -> FATAL 
            ERROR = 2 show  log -> FATAL ERROR 
            WARN =  3 show  log -> FATAL ERROR WARN 
            INFO =  4 show  log -> FATAL ERROR WARN INFO 
            DEBUG = 5 show  log -> FATAL ERROR WARN INFO DEBUG
            -->
      <!-- To write log in file -->
      <appender-ref ref='debug'/>
      <appender-ref ref='file1'/>
    </root>

  </log4net>";

  //
  // Use XmlDocument to load the xml string then pass the DocumentElement to
  // XmlConfigurator.Configure.
  //
  XmlDocument doc = new XmlDocument();
  doc.LoadXml(xml);

  log4net.Config.XmlConfigurator.Configure(doc.DocumentElement);

Here is anohter way to configure log4net with XML via code using XmlDocument to load the xml. The difference from Christian's example is that I am using the XmlConfigurator.Configure overload that takes an XmlElement as a parameter. I also used single tick marks rather than doubling up the double quotation marks. All in all, I think that it is the tiniest bit cleaner.

  string xml =
  @"<log4net>
    <appender name='file1' type='log4net.Appender.RollingFileAppender'>
      <!-- Log file locaation -->
      <param name='File' value='log4net.log'/>
      <param name='AppendToFile' value='true'/>
      <!-- Maximum size of a log file -->
      <maximumFileSize value='2KB'/>
      <!--Maximum number of log file -->
      <maxSizeRollBackups value='8'/>
      <!--Set rolling style of log file -->
      <param name='RollingStyle' value='Composite'/>
      <param name='StaticLogFileName' value='false'/>
      <param name='DatePattern' value='.yyyy-MM-dd.lo\g'/>
      <layout type='log4net.Layout.PatternLayout'>
        <param name='ConversionPattern' value='%d [%t] %-5p  %m%n'/>
      </layout>
    </appender>

    <!-- Appender layout fix to view in console-->
    <appender name='console' type='log4net.Appender.ConsoleAppender'>
      <layout type='log4net.Layout.PatternLayout'>
        <param name='Header' value='[Header]\r\n'/>
        <param name='Footer' value='[Footer]\r\n'/>
        <param name='ConversionPattern' value='%d [%t] %-5p  %m%n'/>
      </layout>
    </appender>

    <appender name='debug' type='log4net.Appender.DebugAppender'>
      <layout type='log4net.Layout.PatternLayout'>
        <param name='ConversionPattern' value='%d [%t] %logger %-5p %m%n'/>
      </layout>
    </appender>
    <root>
      <level value='INFO'/>
      <!--
            Log level priority in descending order:

            FATAL = 1 show  log -> FATAL 
            ERROR = 2 show  log -> FATAL ERROR 
            WARN =  3 show  log -> FATAL ERROR WARN 
            INFO =  4 show  log -> FATAL ERROR WARN INFO 
            DEBUG = 5 show  log -> FATAL ERROR WARN INFO DEBUG
            -->
      <!-- To write log in file -->
      <appender-ref ref='debug'/>
      <appender-ref ref='file1'/>
    </root>

  </log4net>";

  //
  // Use XmlDocument to load the xml string then pass the DocumentElement to
  // XmlConfigurator.Configure.
  //
  XmlDocument doc = new XmlDocument();
  doc.LoadXml(xml);

  log4net.Config.XmlConfigurator.Configure(doc.DocumentElement);
不羁少年 2024-07-28 05:49:02

以下代码介绍如何在代码中配置 AdoNetAppender。 如果要添加过滤器,则需要在 GetAppender() 方法中的appender 实例上添加过滤器实例。 为了在应用程序中使用记录器,建议使用依赖注入来隔离记录器实现和接口。 此外,使用 DI 范围概念使其成为单例以避免对象重新创建。
注意:CrmConfigHelper 类用于访问应用程序配置文件 appSettings 部分。

代码示例如下所示

public sealed class SqlLogger:ILogger
{ 
    private ILog _logger;
    public SqlLogger()
    {            
        log4net.Config.BasicConfigurator.Configure(GetAppender());
        this._logger = log4net.LogManager.GetLogger(CrmConfigHelper.GetString(Constants.LOG4NET_LOGGER_NAME));
    }

    private log4net.Appender.AdoNetAppender GetAppender()
    {            
        log4net.Appender.AdoNetAppender appender = new log4net.Appender.AdoNetAppender();
        appender.ConnectionType = CrmConfigHelper.GetString(Constants.LOG4NET_CONNECTION_TYPE);
        appender.ConnectionString = CrmConfigHelper.GetString(Constants.LOG4NET_DB_CONNECTION);
        appender.BufferSize = CrmConfigHelper.getInteger(Constants.LOG4NET_BUFFER_SIZE);
        appender.CommandText = "INSERT INTO [EventLog] ([Date],[HostName],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @hostname, @log_level, @logger, @message,@exception)";
        appender.CommandType = System.Data.CommandType.Text;
        appender.AddParameter(new log4net.Appender.AdoNetAppenderParameter() { 
            ParameterName = "@log_date",
            DbType = System.Data.DbType.DateTime,
            Size = 100,
            Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}")) as IRawLayout
        });
        appender.AddParameter(new log4net.Appender.AdoNetAppenderParameter()
        {
            ParameterName = "@hostname",
            DbType = System.Data.DbType.String,
            Size = 50,
            Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%property{log4net:HostName}")) as IRawLayout
        });
        appender.AddParameter(new log4net.Appender.AdoNetAppenderParameter()
        {
            ParameterName = "@log_level",
            DbType = System.Data.DbType.String,
            Size = 50,
            Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%level")) as IRawLayout
        });
        appender.AddParameter(new log4net.Appender.AdoNetAppenderParameter()
        {
            ParameterName = "@logger",
            DbType = System.Data.DbType.String,
            Size = 50,
            Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%logger")) as IRawLayout
        });
        appender.AddParameter(new log4net.Appender.AdoNetAppenderParameter()
        {
            ParameterName = "@message",
            DbType = System.Data.DbType.String,
            Size = 4000,
            Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%message")) as IRawLayout
        });
        appender.AddParameter(new log4net.Appender.AdoNetAppenderParameter()
        {
            ParameterName = "@exception",
            DbType = System.Data.DbType.String,
            Size = 2000,
            Layout = new RawLayoutConverter().ConvertFrom(new ExceptionLayout()) as IRawLayout
        });

        appender.ActivateOptions();
        return appender;
    }

    public void Error(Message context)
    {
        _logger.Error(context.ToJsonString());
    }

    public void Error(Message context, Exception exception)
    {
        _logger.Error(context.ToJsonString(), exception);
    }

    public void Warn(Message context)
    {
        _logger.Warn(context.ToJsonString());
    }

    public void Warn(Message context, Exception exception)
    {
        _logger.Warn(context.ToJsonString(), exception);
    }

    public void Info(Message context)
    {
        _logger.Info(context.ToJsonString());
    }

    public void Info(Message context, Exception exception)
    {
        _logger.Info(context.ToJsonString(), exception);
    }
}

public interface ILogger
{
    void Error(Message context);
    void Error(Message context, Exception exception);
    void Warn(Message context);
    void Warn(Message context, Exception exception);
    void Info(Message context);
    void Info(Message context, Exception exception);
}

public sealed class Message
{
    public string RunDate { get; set; }
    public string RunBy { get; set; }
    public string Message { get; set; }

    public string ToJsonString()
    {
        return new JavaScriptSerializer().Serialize(this);
    }
}

The following codes works on how to config AdoNetAppender in codes. If you want to add filter, you need to add filter instance on the appender instance in GetAppender() method. For using logger in your application, Dependency Injection is recommended to be used to isolate logger implementation and interface. In addition, use DI scope concept to make it singleton to avoid object recreation.
Note: CrmConfigHelper class is used to get access to application configuration file appSettings section.

The codes samples are shown below

public sealed class SqlLogger:ILogger
{ 
    private ILog _logger;
    public SqlLogger()
    {            
        log4net.Config.BasicConfigurator.Configure(GetAppender());
        this._logger = log4net.LogManager.GetLogger(CrmConfigHelper.GetString(Constants.LOG4NET_LOGGER_NAME));
    }

    private log4net.Appender.AdoNetAppender GetAppender()
    {            
        log4net.Appender.AdoNetAppender appender = new log4net.Appender.AdoNetAppender();
        appender.ConnectionType = CrmConfigHelper.GetString(Constants.LOG4NET_CONNECTION_TYPE);
        appender.ConnectionString = CrmConfigHelper.GetString(Constants.LOG4NET_DB_CONNECTION);
        appender.BufferSize = CrmConfigHelper.getInteger(Constants.LOG4NET_BUFFER_SIZE);
        appender.CommandText = "INSERT INTO [EventLog] ([Date],[HostName],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @hostname, @log_level, @logger, @message,@exception)";
        appender.CommandType = System.Data.CommandType.Text;
        appender.AddParameter(new log4net.Appender.AdoNetAppenderParameter() { 
            ParameterName = "@log_date",
            DbType = System.Data.DbType.DateTime,
            Size = 100,
            Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}")) as IRawLayout
        });
        appender.AddParameter(new log4net.Appender.AdoNetAppenderParameter()
        {
            ParameterName = "@hostname",
            DbType = System.Data.DbType.String,
            Size = 50,
            Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%property{log4net:HostName}")) as IRawLayout
        });
        appender.AddParameter(new log4net.Appender.AdoNetAppenderParameter()
        {
            ParameterName = "@log_level",
            DbType = System.Data.DbType.String,
            Size = 50,
            Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%level")) as IRawLayout
        });
        appender.AddParameter(new log4net.Appender.AdoNetAppenderParameter()
        {
            ParameterName = "@logger",
            DbType = System.Data.DbType.String,
            Size = 50,
            Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%logger")) as IRawLayout
        });
        appender.AddParameter(new log4net.Appender.AdoNetAppenderParameter()
        {
            ParameterName = "@message",
            DbType = System.Data.DbType.String,
            Size = 4000,
            Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%message")) as IRawLayout
        });
        appender.AddParameter(new log4net.Appender.AdoNetAppenderParameter()
        {
            ParameterName = "@exception",
            DbType = System.Data.DbType.String,
            Size = 2000,
            Layout = new RawLayoutConverter().ConvertFrom(new ExceptionLayout()) as IRawLayout
        });

        appender.ActivateOptions();
        return appender;
    }

    public void Error(Message context)
    {
        _logger.Error(context.ToJsonString());
    }

    public void Error(Message context, Exception exception)
    {
        _logger.Error(context.ToJsonString(), exception);
    }

    public void Warn(Message context)
    {
        _logger.Warn(context.ToJsonString());
    }

    public void Warn(Message context, Exception exception)
    {
        _logger.Warn(context.ToJsonString(), exception);
    }

    public void Info(Message context)
    {
        _logger.Info(context.ToJsonString());
    }

    public void Info(Message context, Exception exception)
    {
        _logger.Info(context.ToJsonString(), exception);
    }
}

public interface ILogger
{
    void Error(Message context);
    void Error(Message context, Exception exception);
    void Warn(Message context);
    void Warn(Message context, Exception exception);
    void Info(Message context);
    void Info(Message context, Exception exception);
}

public sealed class Message
{
    public string RunDate { get; set; }
    public string RunBy { get; set; }
    public string Message { get; set; }

    public string ToJsonString()
    {
        return new JavaScriptSerializer().Serialize(this);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文