How do I configure log4net such that properties of

2019-02-18 07:25发布

I am attempting to provide a means to log errors that occur in our Flex client by providing a SOAP web service which takes as a single parameter a LogMessage object.

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 should a client side error surface and the LogClientError method is invoked which logs the error via log4net.

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

Currently this prints the fully qualified name of the LogMessage class, so my current assumption is that log4net simply calls .ToString() on the object that is passed in.

However, what I really want to be able to do is map each property in the LogMessage class to a pattern so that log4net will correctly write out the desired information. I would like to do this in such a manner that the typical appenders (DB, File, SMTP) are all still supported.

How do I configure log4net such that properties of an object can be mapped to the log output?

2条回答
SAY GOODBYE
2楼-- · 2019-02-18 07:51

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

The other route, which would be more reusable, is to subclass the patternlayout class. Your custom pattern layout could then support a special syntax for naming properties which the layout could use to reflect on the incoming message object.

查看更多
做自己的国王
3楼-- · 2019-02-18 07:51

If LogMessage is partial you could create a ToString() method on LogMessage.

查看更多
登录 后发表回答