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.
It works for me if I extend LogFormatter (instead of implementing ILogFormatter) and overriding Format():
Along with this configuration:
If you don’t pass an
ErrorEntryin to Logger.Write() then aLogEntrywill be constructed and not a customErrorEntry. So, you should always pass anErrorEntryin and not use the other overloads. E.g.: