Enterprise Library Logger.Write Not Working?
Using Logging Application Block provided by Enterprise Library is a common way of logging exceptions in .NET applications. It’s easy to setup and to use. One problem I had a while ago was that it worked while on my development PC, but stopped working once it’s deployed.
What I was doing was to use it to write event entries in windows event log as part of the exception handling in an ASP.NET web application. Nothing fancy. It worked fine on my PC, but it didn’t write anything to the event log when Logger.Write is executed. No exception was thrown from that line of code either. The program just went through as if that line of code was skipped.
It turned out to be a permission issue. When I debug the application in Visual Studio, it was running under my windows account credential which had permission to create Event Source in the system. However, when the application was deployed to the server, it was running under the default account which was “NT Authority\Network Service” in my case. By default, that account doesn’t have permission to create new Event Source in the system. It does have permission to write event entries though. So, when Logger.Write was executed, it couldn’t proceed because the Event Source wasn’t there, and the program didn’t have permission to create one. Actually, it makes sense to not throw any exception on this line, because people like me usually use this to log exceptions, and if the logging process itself throws exceptions, there is probably nowhere else we can handle and/or log them.
Now, how this can be resolve? The solution is quite simple. As long as the Event Source is created ahead of time, it will be no problem. What I did was to create required Event Source(s) on server through CMD.exe. Here is a sample command:
eventcreate /ID 1 /L Application /T information /SO "My Event Source" /D "My Event Source is created."
This command will create an event entry in the Application log. If the Event Source doesn’t exist, it will be created during the process. This is only required when the application is deployed to the server first time. So, I usually do it manually on server. However, if that’s not the case, you can build it into the installation package, and when the application is installed, the Event Source is created as well. Of course, you can use .NET to create Event Source in your installer too.
Tags: .NET, Enterprise Library, Logger