views:

140

answers:

3

I'm using a third party application that provides an API to use their event logging system. I want to catch an exception at the method level in my code and pass that exception to the third party's event logging system. But I dont know how to extract the eventid, category and EventType from the standard Exception object so i can pass it to Write(string message, string category, int eventID, EventType eventType).

public EventLogs 
{
    private EventLogs()
    {
    }    
    public static void Write(EventLogEntry entry)
    {
        try
        {
            if (Globals.IsNullorEmpty(entry.MachineName))
            {
                entry.MachineName = Environment.MachineName;
            }
            if (!Globals.IsNullorEmpty(entry.Message))
            {
                entry.Message = Globals.HtmlEncode(entry.Message);
            }
            CommonDataProvider.Instance().WriteEventLogEntry(entry);
        }
        catch
        {
        }
    }

    public static void Write(string message, string category, int eventID, EventType eventType)
    {
        Write(message, category, eventID, eventType, -1);
    }

    public static void Write(string message, string category, int eventID, EventType eventType, int settingsID)
    {
        Write(new EventLogEntry { Message = message, Category = category, EventID = eventID, EventType = eventType, SettingsID = settingsID });
    }
}
A: 

Your exception doesn't contain that information.

The exception that is generated and logging to the Event Log on your windows box are two different concepts.

The Event Log takes the parameters you're describing and uses it when generating the log entry that you can then view in the Event Viewer on your machine.

Here's the msdn article on writing to the event log, it has more information on what those parameters are used for.

Joseph
+1  A: 

The parameters for the Write() call don't have any specific relation to .NET exception objects. Those parameters, however, correspond closely with items stored in the normal windows event log. Depending on the logging system you are using, this data may or may not end up in the windows event log. It is likely that their design is meant to mimic the windows event log.

What you specify for the parameters is up to your needs and goals. Some people like to store the results of Exception.ToString() in the message, the name of the application or app domain in the category, and a user-defined id code for the eventID.

Other potentially useful bits of information you might want to store in the message are the typename of the exception, the stack trace, and binary data associated with the exception.

Last, it is atypical to store a 0 as the eventID for errors. This might throw-off other IT personnel using their own log scanning tools.

System.Exception @ MSDN
System.Exception Properties @ MSDN

meklarian
A: 

You need to explicitly catch the exception into a variable and then you can interrogate it, pull it apart, to write its pieces elsewhere.

catch(Exception exc)
{
    Console.Write(exc.Message);
}

System.Exception is the base class for all exceptions so it can represent any exception that is thrown. See all the members of the Exception class from MSDN.

Edit: The standard .NET Exception object doesn't have eventid, category and EventType, so you have to decide what these will be before writing to the third-party logging utility.

For example, you might choose to categorize exceptions based on the type of exception because you can distinguish between them when catching like this,

catch(FileNotFoundException fnfExc)
{
    Logger.Log(fnfExce.Message, CategoryEnum.DiskError);
}
catch(FilePermissionError permExc)
{
    Logger.Log(permExc.Message, CategoryEnum.DiskError);
}
catch(Exception exc)  { //catches anything else not caught above
    Logger.Log(exc.Message, CategoryEnum.Uknown);
}
John K