Using a custom logentry class with a custom logformatter
c#, enterprise-library, logging
Solution
It works for me if I extend LogFormatter (instead of implementing ILogFormatter) and overriding 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";
}
}
Along with this configuration:
<?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>
If you don't pass an `ErrorEntry` in to Logger.Write() then a `LogEntry` will be constructed and not a custom `ErrorEntry`. So, you should always pass an `ErrorEntry` in and not use the other overloads. E.g.:
ErrorEntry errorEntry = new ErrorEntry();
errorEntry.UserId = "ME!";
errorEntry.Message = "Default message";
errorEntry.Categories.Add("General");
Logger.Write(errorEntry);
Problem
I have a custom LogEntry class - ErrorEntry which derives from LogEntry. It has a few public properties (a class object is also one of the properties) where I wish to put in additional data. ``` public class ErrorEntry : LogEntry { public string UserId {get; set; } public SessionDetail SessionInfo {get; set; } } ``` I also have a custom formatter - 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; } } ``` I have set up the web.config settings with the custom formatter and it works fine till the statement where it checks if the variable is of type ErrorEntry - over here it never becomes true and goes into the else block. I am a bit confused here - is there something else that I need to do? Is this implementation supported by a custom formatter class? I am using Enterprise Library 3.1.