Logging exceptions to database in NServiceBus

Posted by IGoor on Stack Overflow See other posts from Stack Overflow or by IGoor
Published on 2010-05-28T13:40:15Z Indexed on 2010/05/28 13:42 UTC
Read the original article Hit count: 283

If an exception occurs in my MessageHandler I want to write the exception details to my database. How do I do this?

Obviously, I cant just catch the exception, write to database, and rethrow it since NSB rollbacks all changes. (IsTransactional is set to true)

I tried adding logging functionality in a seperate handler, which I caledl using SendLocal if an exception occured, but this does not work:

public void Handle(MessageItem message)
{
    try
    {
        DoWork();
    }
    catch(Exception exc)
    {
        Bus.SendLocal(new ExceptionMessage(exc.Message));
        throw;
    }
}

I also tried using Log4Net with a custom appender, but this also rolled back.

Configure.With()
.Log4Net<DatabaseAppender>(a => a.Log = "Log")

appender:

public class DatabaseAppender : log4net.Appender.AppenderSkeleton
{
    public string Log { get; set; }

    protected override void Append(log4net.Core.LoggingEvent loggingEvent)
    {
        if (loggingEvent.ExceptionObject != null)
            WriteToDatabase(loggingEvent.ExceptionObject);
    }
}

Is there anyway to log unhandled exceptions in the messagehandler when IsTransactional is true?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about database

Related posts about logging