views:

1062

answers:

3

By default, ASP.NET records all uncaught exceptions to system Event Log. I’m aware of the fact that one should have a proper logging facility in place, but this is better than nothing and it serves well as a temporary solution.

I would like to be able to filter efficiently the events in the log. I learned that, when logging programmatically, you can set a custom value for the Source column in the event log via:

EventLog eventLog = new EventLog("Application");
eventLog.Source = "My custom name";
eventLog.WriteEntry("Some error description ...", EventLogEntryType.Error);

However, ASP.NET sets this value to "ASP.NET" followed by its version. I briefly checked the documentation of web.config, but did not find an obvious place to change it. I wonder if it can be changed at all.

A: 

You may want to handle uncaught exceptions in your global.asax, and then log the Exception programmatically as so:

void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError().GetBaseException();
    // logging code here
}
davogones
Thank you. I know that this is a better of doing it in general, but I was looking for a simple configuration switch.
Jan Zich
+1  A: 

It seems that using the source property is not such a good idea. Originally, I thought that it was a free form text. But I just found out that it’s something which has to be registered via the RegisterEventSource(...) Win32 API function, and this appears to work only when the application runs under the admin privileges. .NET silently creates a new source for you, but if you are not an admin, it throws an exception. So overall, using an ad-hoc source name inside an ASP.NET may require some pre-registration which would introduce another step into deployment.

Jan Zich
+1  A: 

Your best bet is to use the source property as intended, but use an installer class in your installer to set up the registry at install time (under Admin), eg.:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;

namespace InstallerClasses
{
    [RunInstaller(true)]
    public partial class EventLog : Installer
    {
        private EventLogInstaller eventLogInstaller;

        /// 
        /// Creates the event log for MyApp
        /// 
        public EventLog()
        {
            InitializeComponent();

            // Create an instance of an EventLogInstaller.
            eventLogInstaller = new EventLogInstaller();

            // Set the source name of the event log.
            eventLogInstaller.Source = "MySource";

            // Set the event log that the source writes entries to.
            eventLogInstaller.Log = "Application";

            // Add myEventLogInstaller to the Installer collection.
            Installers.Add(eventLogInstaller);
        }
    }
}

And make sure it gets run as a Custom Action in your installer.

Chris KL
Thank you. This confirms my finding that the source property is not meant to be used as an ad-hoc string.
Jan Zich