我有一个自定义的LogEntry类 - ErrorEntry,它派生自LogEntry。它有一些公共属性(类对象也是其中一个属性),我希望在其中添加其他数据。
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块。
我在这里有点困惑 - 我还需要做些什么吗?这个实现是否由自定义格式化程序类支持?
我正在使用Enterprise Library 3.1。
答案 0 :(得分: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 & Warnings">
<listeners>
<add name="FlatFile TraceListener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
<startup>
<supportedRuntime version="v2.0.50727" />
</startup>
</configuration>
如果您未将ErrorEntry
传递给Logger.Write(),则会构建LogEntry
而不是自定义ErrorEntry
。因此,您应始终传递ErrorEntry
并不使用其他重载。 E.g:
ErrorEntry errorEntry = new ErrorEntry();
errorEntry.UserId = "ME!";
errorEntry.Message = "Default message";
errorEntry.Categories.Add("General");
Logger.Write(errorEntry);