c# - 使用自定义 logentry 类和自定义 logformatter

标签 c# logging enterprise-library

我有一个自定义的 LogEntry 类 - ErrorEntry,它派生自 LogEntry。它有一些公共(public)属性(类对象也是属性之一),我希望在其中放入额外的数据。

public class ErrorEntry : LogEntry
{
 public string UserId {get; set; }
 public SessionDetail SessionInfo {get; set; }
}

我还有一个自定义格式化程序 - MyFormatter

[ConfigurationElementType(typeof(CustomFormatterData))]
public class MyFormatter : ILogFormatter
{
  public MyFormatter(NameValueCollection nvc)
  {
     //not used
  }

  public string Format(LogEntry log)
  {
    string strEntry;
    if(log is ErrorEntry)
    {
       //use properties of ErrorEntry
       //populate returnString
    }
    else
    {
       throw new ArgumentException("Not supported");
    }
    return strEntry;
  }
}

我已经使用自定义格式化程序设置了 web.config 设置,它工作正常,直到它检查变量是否为 ErrorEntry 类型的语句 - 在这里它永远不会变为 true 并进入 else block 。

我在这里有点困惑 - 我还需要做些什么吗?自定义格式化程序类支持此实现吗?

我正在使用 Enterprise Library 3.1。

最佳答案

如果我扩展 LogFormatter(而不是实现 ILogFormatter)并覆盖 Format(),它对我有用:

[ConfigurationElementType(typeof(CustomFormatterData))]
public class MyFormatter : LogFormatter
{
    public MyFormatter(NameValueCollection nvc)
    {
        //not used
    }

    public override string Format(LogEntry log)
    {
        ErrorEntry errorEntry = log as ErrorEntry;

        if (errorEntry != null)
        {
            return "This is the string with the custom values: " + errorEntry.UserId;
            //use properties of ErrorEntry
            //populate returnString
        }

        return "Not supported";
    }
}

连同此配置:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </configSections>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add fileName="trace.log" header="----------------------------------------"
        footer="----------------------------------------" formatter="Custom Formatter"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
        traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
        name="FlatFile TraceListener" />
    </listeners>
    <formatters>
      <add type="CustomFormatters.MyFormatter, CustomFormatters, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        name="Custom Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="FlatFile TraceListener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="FlatFile TraceListener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <startup>
    <supportedRuntime version="v2.0.50727" />
  </startup>
</configuration>

如果您不将 ErrorEntry 传递给 Logger.Write(),则将构造一个 LogEntry 而不是自定义 ErrorEntry .因此,您应该始终传递一个 ErrorEntry 而不是使用其他重载。例如:

ErrorEntry errorEntry = new ErrorEntry();
errorEntry.UserId = "ME!";
errorEntry.Message = "Default message";
errorEntry.Categories.Add("General");

Logger.Write(errorEntry);

关于c# - 使用自定义 logentry 类和自定义 logformatter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13963263/

相关文章:

linux - 使用 iptables 记录所有到关闭端口的连接

c# - 将 MEF 与企业库一起使用

c# - 在 web.config 中添加第三个端点显示错误

c# - 根据值更改颜色

c# - 图像处理 - 在不去除的情况下减少物体厚度

caching - System.Web.Caching 与 Enterprise Library 缓存 block

c# - Unity 记录静态方法 AOP 风格或无接口(interface)

c# - 如何判断文件复制何时结束?

c++ - 记录极少量数据的最有效方法是什么?

java - 如何将Java服务器日志写入单独的日志文件