1

I am trying to implement Log4Net file appender in asp.net. I have been successful implementing it. However I am not sure about correct architecture to implement it.

I can add a logger in each page and log information. However, I was thinking to centralize the logger class. May be implement a singleton pattern. But i was wondering what will happen if a request for same page comes from two different browsers. I can implement Thread Static and then every page instead of initializing their own logger would use this centralize logger class to log.

I suppose the log4net file appender or rolling file appender using a queue mechanism to write to the log file. Because only one handle of the file can be acquired to write to a file.

Can anyone help me in this regard. Am i going the right way or i will have issues down the road when there will be tens and hundreds of requests coming from different browsers.

ItsZeus
  • 142
  • 12
  • This looks to be a pretty decent way to use Log4Net .. https://code.msdn.microsoft.com/How-to-use-Apache-log4net-0d969339 – sundeep Jul 18 '15 at 04:37
  • This is for asp.net mvc. But I am using asp.net webforms. do you think singleton pattern with thread static would not cause any issue ? Its implemented how i am implementing it. By having a logger in every controller. So a request comes and a logger is created. so a logger is created everytime a request comes and then disposed off. – ItsZeus Jul 18 '15 at 05:14
  • Check these links ... http://stackoverflow.com/questions/9071784/should-i-declare-log4net-logger-once-per-class-or-in-base-class and http://stackoverflow.com/questions/166438/what-would-a-log4net-wrapper-class-look-like – sundeep Jul 18 '15 at 05:58
  • Thanks sundeep ! I'll post my review once i go through the blog. But seems this is the way to go – ItsZeus Jul 18 '15 at 05:58

1 Answers1

1

I recommend not to use a singleton, but instead to use a logger for each controller and class that you want to log from. Loggers are cheap to create and cached by log4net - and even more so if you declare them as static within the class - and by having one per class you can change logging per class or namespace by changing the log4net configuration at runtime - say to enable some debug logging to aid diagnosing a problem in production, or to turn down some logging which is unexpectedly noisy. You can do this without recycling your app if you have your log config in a separate file.

Also if you're going to use file logging, make sure you use MinimalLock

Community
  • 1
  • 1
stuartd
  • 70,509
  • 14
  • 132
  • 163
  • Thanks a lot your comments makes a lot of sense. Kind of reply i was looking for. – ItsZeus Jul 20 '15 at 07:25
  • The reason why i wanted to use a centralized class was to wrap log4net logger in a custom class so if later on i wanted to switch logger i wouldn't have to change code in all of the classes. That would be a lot of work. What do you suggest for this ? – ItsZeus Jul 20 '15 at 07:26
  • You can [wrap log4net](http://stackoverflow.com/questions/166438/what-would-a-log4net-wrapper-class-look-like) and use the wrapper in your classes. – stuartd Jul 20 '15 at 08:47
  • I did that. However if i turn on location what it does it, it gives me location (line number and class name etc) of that particular singleton class where logging is actually happening instead of showing location where the call was made – ItsZeus Jul 22 '15 at 07:50
  • @ItsZeus and that's what you get for using a singleton. The logger name should tell you the class, and the context (ie the content of the log) where it was called from - you will lose line numbers in release builds anyway. – stuartd Jul 22 '15 at 08:25