I have a common logging project that uses log4net to record logging events. Since it's a common project, it's configured in a non-standard way: almost everything is set via C# code.
Now, I have an AdoNet appender configured in this manner, which includes logging the method that triggered the log event. It is created as part of logging initialization and it's defined as follows:
appender.AddParameter(new AdoNetAppenderParameter()
{
ParameterName = "@Method",
DbType = DbType.String,
Size = 255,
Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%method")) as IRawLayout
});
Since I'm tying into the log4net %method
property, this will automatically pull the method name that triggered the logging event and send it to the database in a parameter called @Method
which will eventually be inserted into a database table that has a column called Method
.
However, I'm adding some new functionality to globally handle exceptions and log them. When an exception bubbles up to the top of the call stack from anywhere, it will be passed to this new global method so it can be logged. I have access to the exception, so I can see the controller and method that caused this exception. I could easily add this as a log4net custom property (mapped to something like %property{ExceptionMethod}
). My problem is overriding (or overwriting) log4net's %method
property with my own custom property.
So, how can I have log4net conditionally choose between %method
and %property{ExceptionMethod}
when sending the data through the AdoNet appender to log this information under the Method column of my logging database table? Is this even feasible?