I have an ASP.NET App that uses a .NET library. I want to enable logging to somewhere to check later the error messages.
I will log ASP.NET app and .NET library, to the same place or to different places. I don’t know how to do it.
I prefer to use System.Diagnostics. from .NET Framework instead of 3rd party frameworks, but I don’t know if this is the better solution.
I want to log error messages and, sometimes, custom messages.
Thank you!
There are plenty logging frameworks for .NET, If you want to have powerful and flexible logging in your application then you should use one of 3rd party frameworks, I prefer log4net. There are also several ways for doing this using only classes of .Net Framework.
For example you can use these approaches:
The simplest way will be creation of your personal implementation of logger, for example you can just use File.AppendAllText at the place you want to log something to somewhere. If you want to use SQL database as a storage for your log entries you can use SqlCommand class filled with appropriate insert query or you can define another way of logging. But this way isn’t recommended because there are many chances that others who will support your code will know usage of well known frameworks & practices and there aren’t any chances that they will know your implementation, so they probably will need to read bigger amount of code.
You can use EventLog class. We have used it to log critical errors (like unhandled exceptions) happened in our services and ASP.NET applications. It will allow you to have unconditional logging to system wide storage. That storage will be accessible from Microsoft Management Console (mmc) and that storage will be configurable.
You can also use something like Debug.WriteLine or Trace.WriteLine for logging. In that case you will have ability to configure log listeners via editing of web.config file. You will have ability to enable or disable of logging, change ways of logging (log to file, to event log or to something else).