简体   繁体   中英

Logging exceptions in asp.net

In my team there is discussion about web application exception logging, regarding whether we should log to a text file or the event log.

Can anyone please tell me which is the better way of exception logging occuring in the application? Either one of the two I've already mentioned or is there a better option?

Don't reinvent the wheel - use an already existing logging library/tool such as log4net or ELMAH instead of writing your own logging library. Both give you multiple choices for where you log exceptions to, it's up to you which you prefer.

Two things to consider:

1 - Where do you log to if you're logging to the database, but the database is unavailable / under heavy load?
2 - Windows Event Logging is very light-weight and event log forwarding means you can automagically aggregate logs from multiple machines without writing any code .

Another great and easy to use Logging-Library is NLOG

From the site:

"Supported targets include:

  • Files – single file or multiple, with automatic file naming and archival
  • Event Log – local or remote
  • Database – store your logs in databases supported by .NET
  • Network – using TCP, UDP, SOAP, MSMQ protocols
  • Command-line console – including color coding of messages
  • E-mail – you can receive emails whenever application errors occur
  • ASP.NET trace

… and many more

Other key features:

  • very easy to configure, both through configuration file and programmatically
  • easy-to-use logger pattern known from log4xxx
  • advanced routing using buffering, asynchronous logging, load balancing, failover, and more
  • cross-platform support: .NET Framework, .NET Compact Framework and Mono (on Windows and Unix)"

I prefer a text file because it allows for more flexibility and easier navigation. I suppose it's a matter of preference, but the navigation between errors in the Windows event log seems very cumbersome. In addition to the cumbersome navigation it includes errors that you don't care about. If you can define the format and the content then it's much more efficient.

At the app I work, we are used to log every exception at the database, so this way is easier to link the exceptions details to the "user error report" they are requested to fill (which is at our the default error page). It helps us on statistics and as already said, linking exception details to the user description of the error (like, which steps he took to get that error, etc...)

Disclaimer: i am the developer of KissLog.net logging application.

log4Net and Elmah are very good, but they have limited capabilities.

Elmah doesn't support trace / log messages, and logs only the exceptions, and log4Net doesn't provide a user interface to navigate through the logs.

You can try to use KissLog.net , it is an application where i tried to aggregate all the necessary logging features (capture errors, log / trace messages, easy to use user-interface)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM