4

私はカスタムLogEntryクラスを持っています-LogEntryから派生するErrorEntry。追加のデータを入れたいパブリックプロパティがいくつかあります(クラスオブジェクトもプロパティの1つです)。

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ブロックに入ります。

私はここで少し混乱しています-私がしなければならないことが他にありますか?この実装はカスタムフォーマッタークラスでサポートされていますか?

EnterpriseLibrary3.1を使用しています。

4

1 に答える 1

2

(ILogFormatterを実装する代わりに)LogFormatterを拡張し、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>

ErrorEntryLogger.Write()にを渡さない場合LogEntry、カスタムではなく、が作成されErrorEntryます。したがって、常にErrorEntryインを渡し、他のオーバーロードを使用しないようにする必要があります。例えば:

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

Logger.Write(errorEntry);
于 2012-12-20T01:42:48.173 に答える