Log4Net: writing a C # object (except for an exception) to the log?

I saw in Log4Net you can write a message and pass an exception object as the second parameter.

Is it possible to send another type of object to log4net so that I can see the values โ€‹โ€‹in my log of my object?

If I have an instance of a class that is populating, and I sent an instance for writing,

Is it possible?

I would like to see a tutorial, but I can not find it

Any help really appreciated

+4
source share
3 answers

You can implement a custom formatter . (First try Google to see if anyone has done something that matches your scenario).

+2
source

You can always write it yourself, which gives you a little control. You can use Reflection to get the type of an object, get the properties, and write them as Debug or Info.

You can extend the method that gives you properties to write out just what you want and run recursively n-levels deep into the object or until it has no properties.

+1
source

The various ILog interface ILog accept an object as a "message" parameter, so the easiest way to get an object "recorded" by log4net is to implement the ToString() method.

Another way is to provide an implementation of IObjectRenderer . I am not sure how to register this in the configuration file, but it shows how to do this in the code.

If you need an object as a separate object in the application, you should use the event log context (also works well with the ToString() implementation.

0
source

All Articles