Exception & Logging Application Block: Returning the ErrorID

Topics: Exception Handling Application Block, Logging Application Block
Jul 14, 2012 at 7:21 PM

Hi there,


I can see, for Logging/Exception Handling, the the WriteLog stored procedure returning a ErrorID as a output parameter. How to capture this at C# code?

Many Thanks,


Jul 15, 2012 at 5:43 AM
Edited Jul 15, 2012 at 5:46 AM

I think you are referring to the stored procedure used by the FormattedDatabaseTraceListener.  The public Write methods of the abstract LogWriter class do not return a value so returning data from the Logging Application Block is not part of the design.  For the purpose of users of the Logging Application Block the LogID returned by the WriteLog stored procedure is an internal implementation detail.

If you really need to get at the ID of the newly inserted log record there are a few approaches that you could use:

  1. Modify the Logging Application Block source code to potentially return a value (although this could be messy and would only really have meaning if a database trace listener is used).
  2. Create a custom trace listener that sets the LogID so that it is available later by the caller.  This could be a ThreadStatic variable or some other type of global variable based on a thread ID or a custom request ID.  Another approach would be to have the custom trace listener add a new ExtendedProperties value for the LogID.  For this to work the Write method which accepts a LogEntry would have to always be used.
  3. Use a custom approach without Enterprise Library that aligns with your exact needs.

None of the approaches is elegant.  If pushed I would go with option 2 and write a facade so that callers can easily get the LogID back.  Assuming that the custom trace listener adds the LogID to the ExtendedProperties then the facade might look something like:

    public int WriteFacade(string message, string category)
        LogEntry logEntry = new LogEntry() 
            Message = message, 

        // Can optimize and keep an instance reference to this
        var logger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

        if (logEntry.ExtendedProperties.ContainsKey("LogId"))
            object rawId = logEntry.ExtendedProperties["LogId"];
            int logId;

            if (rawId != null && int.TryParse(rawId.ToString(), out logId))
                return logId;

        return 0;

Randy Levy
Enterprise Library support engineer

Jul 15, 2012 at 6:34 AM

awwww thats exactly what I was looking into....


Thanks Levy :-)

Mar 18, 2014 at 4:48 PM
Thank you for confirming.