1

I have a REST-webservice that always is called this way:

http://rest.company.com/rest/path/?SPHostUrl=www.origin.com
http://rest.company.com/rest/path/?SPHostUrl=www.anotherorigin.com

I want to be able to create different log files for each origin from the SPHostUrl-Parameter of the HttpContext.

Just assume a HtttpContext is always available when I log.

The golden solution would be to have logfiles like:

applicationlog.origin.log and applicationlog.anotherorigin.log

Is it possible to do this using a custom RollingFileAppender?

If this is not possible: Is there a way I can automatically add the url as part of the patternlayout without having to modify every Log.Debug(), etc. lines?

(note) The SPHostUrl is quite dynamic. Currently about 10 different origins and this can change. So creating an appender for each origin in the Web.Config is not an option for me.

Ole Albers
  • 8,715
  • 10
  • 73
  • 166
  • Possible duplicate of [Log4Net: Programmatically specify multiple loggers (with multiple file appenders)](https://stackoverflow.com/questions/308436/log4net-programmatically-specify-multiple-loggers-with-multiple-file-appenders) – Ipsit Gaur May 16 '18 at 11:09
  • No. That question was about deciding programmatically on each Log-Entry which file to use. I am searching for a solution where I do not have to modify my (1000+) Lines of Log-Code – Ole Albers May 16 '18 at 11:19
  • 1
    although this does not answer your question, I would consider using log4net [NDC (Nested Data Context)](https://logging.apache.org/log4net/release/manual/contexts.html). A good and short usage example is shown [here](https://stackoverflow.com/a/17344012/6144699). You would still have just one log file, but each row will be easily identified by the origin. You don't have to change _every_ log.Debug() line, just enclose your methods in a `using(log4net.ThreadContext.Stacks["NDC"].Push(origin))` – PSkalka May 16 '18 at 15:57

0 Answers0