How to configure log4net so that object properties can be mapped to log output?

I am trying to provide tools for reporting errors that occur in our Flex client by providing a SOAP web service that takes a LogMessage object as the only parameter.

public class LogMessage { public string Message { get; set; } public string Exception { get; set; } public string Version { get; set; } public string User { get; set; } } 

This object is populated by the Flex client if the client-side error surface and the LogClientError method are called, which logs the error through log4net.

 [WebMethod()] public void LogClientError(LogMessage message) { rollingLogger.Error(message); } 

This currently prints the full name of the LogMessage class, so my current assumption is that log4net just calls .ToString () on the passed object.

However, what I really want to do is map each property in the LogMessage class to a template so that log4net correctly writes the necessary information. I would like to do this in such a way that typical applications (DB, File, SMTP) are still supported.

How to configure log4net so that object properties can be mapped to log output?

+4
source share
2 answers

log4net has two paths you can use. The first would be to create a specialized object renderer for LogMessage instances. You need to implement the IObjectRenderer interface and register the implementation .

Another route that will be more reusable is to subclass the patternlayout class. Then your own template template can support a special syntax for naming properties that the layout can use to display a message on the incoming object.

+3
source

If LogMessage is partial, you can create the ToString () method in LogMessage.

0
source

Source: https://habr.com/ru/post/1286729/


All Articles