0

I am using log4net for logging calls to an API. Many calls. The methods I am calling have multiple megabytes of data for request/response pairs, and it is very hard to read logs that have multiple calls written to the same file, no matter what logging pattern I use. So, I feel the best approach is to log to multiple files.

I am having a hard time figuring out how to get log4net to do this, or if it even supports it.

From the Log4Net FAQ - Can the outputs of multiple client request go to different log files?

Many developers are confronted with the problem of distinguishing the log output originating from the same class but different client requests. They come up with ingenious mechanisms to fan out the log output to different files. In most cases, this is not the right approach.

It is simpler to use a context property or stack (ThreadContext) ... Thereafter, log output will automatically include the context data so that you can distinguish logs from different client requests even if they are output to the same file.

I looked at the documentation on Contexts and Context Properties. It seemed Event Context fit best, but I tried reading docs for other Contexts too. It seems they just allow me to put additional properties that end up in my log files, rather than being a component of a log file name, or allow me to automatically append to different files.

Is there a way to configure appenders to create different files for different context properties or context stack levels, etc?

Edit:

I am using log4net via Castle Windsor Logging facility, and I'm considering switching to NLog to solve this problem.

NLog seems to support this behavior by using the {logger} layout renderer in the File target's fileName property. I can effectively set this property by making a child logger with Windsor's ILogger.CreateChildLogger method, and setting {logger.shortName=True}.

See:

http://nlog-project.org/forum#nabble-td1685989

I'd still prefer to use log4net if possible, since the project I am testing uses it. Maybe my NLog example can give someone inspiration on how this could be done on log4net, and maybe they can help me figure it out :)

Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183

1 Answers1

1

This article may be of interest to you: Log4Net: Programmatically specify multiple loggers (with multiple file appenders)

Also if you are only worried about readability there may be log file viewers that can seperate out log entries by thread name.

Another possibility you have is to log the entries in a database including your thread name and these entries are easily filtered using sql.

Community
  • 1
  • 1
Cole W
  • 15,123
  • 6
  • 51
  • 85
  • I think specifying the loggers programatically takes away some of the usefulness of log4net's design. I'd like this to be configurable independently of my code, with the only code changes being to instrument the logging to have different contexts/properties per request. I'm starting to think that the rest of your answer (filter later with a log viewer tool) is the only choice with log4net in this scenario. +1 for that part. – Merlyn Morgan-Graham Apr 28 '11 at 03:15