5

I have a C# console application project which uses log4net as the logging library. Throughout the application there are logging statements (log.Debug(), log.Error(), etc.), which are printed to the console as the program runs.

What is the best way to change the logging statements in a production environment, to minimize time of execution caused by logging? I would like some of the statements to not print out at all, and for some logging statements to only be printed out during production.

I am thinking of adding new setting in the Web.config file which determines how the logging changes. However I think I'd also need to override the log.Debug() and log.Error() methods to work with the new setting, but am unsure how to do this. Can anyone advise?

user2181948
  • 1,646
  • 3
  • 33
  • 60
  • 2
    Have you configured log4net to read configuration from the app.config file? If so you should be able to set the log level to OFF or ERROR, etc http://stackoverflow.com/questions/8926409/log4net-hierarchy-and-logging-levels – Andrew Harris May 08 '17 at 05:08

2 Answers2

1

There is no setting in the Web.config file which can tell log4net to do things differently depending on your build.

However, you can have multiple Web.config files and deploy the one you want depending on the build or deployment environment. The following links might help:

Community
  • 1
  • 1
FortyTwo
  • 2,414
  • 3
  • 22
  • 33
0

You should probably have abstracted away your logging library from your application, that way you would have control over your logging behaviour, and make your project loosely coupled to your log4net dependency.

Its probably not the short one-line answer you was hoping for, but i would highly recommend using this design pattern, since it will save you lots of time down the line. Heres a brief article that shows the concept

BobbyTables
  • 4,481
  • 1
  • 31
  • 39
  • So the general idea would be instead of calling log.Debug() directly in a class, you call a method in another class (eg. 'log()') which then calls log.Debug() with different messages etc. depending on a setting Web.config? Is there a standard/best practice way of doing this? – user2181948 May 08 '17 at 21:22