8

We have a couple of long lived objects each of the same class in the system. There are only about 5 or 6 and they are connections to outside systems. I want each of those instances to have their own file that they can log to.

What is the best way to do this? I can only see adding loggers programatically as the answer right now.

UPDATE: I want to avoid using the configuration file because if I add a new connection to a different remote host then i want its log output to go to file named after the connection without having to first hack around in a config file. It would be nice if it was done automagically. I don't know maybe this can be achieved in a config file once off and that you don't need to edit it everytime.

uriDium
  • 13,110
  • 20
  • 78
  • 138

3 Answers3

1

I can actually see a couple of possibilities:

  1. Assign each logging to a different category for each connection and append logs for each category to different files.
  2. Create new loggers programmatically as you suggested.
  3. Use dependency injection to inject as many separate logger as you need.
Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
  • I am already using dependency injection. I would prefer to not have to use the config file because then I need to change our config file everytime I want to add a new connection. I would rather that happen automatically. – uriDium May 26 '11 at 09:27
  • Then [AOP](http://code.google.com/p/postsharp-user-plugins/wiki/Log4PostSharp) would come to your aid via simplifying config file generation to simple attribute usage but then your initial setup time will be a bit complicated (but will be very easy to use). – Teoman Soygul May 26 '11 at 09:30
1

The best answer IS programatical logging, i went through the same problem and found it as the only solution.

For more help check this question (mine) StackOverFlow Question posted by me

and i thing yours and my problem are the same, dynamic loggers, re create loggers, dispose loggers and all that stuff at runtime :)

if any more questions, do ask, will be glad to help since i have gone through all that :)

Community
  • 1
  • 1
Basit Anwer
  • 6,742
  • 7
  • 45
  • 88
0

I have an article that might help:

http://horth.com/blog/?p=165

This is about changing a logfile at runtime. What you could do is pass in the file name for each instance into your log4net file. That way you could create a log file for each instance of your class.

As for your edit about not wanting to use a config file, this method gets around that issue so that you can use a config file but still have the flexibility you want.

IAmTimCorey
  • 16,412
  • 5
  • 39
  • 75
  • I think this works if process wants to log to a different file. All the different logs that I want to create all belong to the same process. – uriDium May 30 '11 at 07:21
  • @uriDium - The example is for one file per process. However, you can change that to one file per instance (of the class). Just change this line to take your instance name or another custom property that you passed in: `logFile = Process.GetCurrentProcess().ProcessName+".log";` Whatever you pass in to this logFile variable will be the name of your file to log to. This means you could create a different log file every time you initialize a class, which is what you want to do. – IAmTimCorey May 30 '11 at 19:15