将异常记录到 NServiceBus 中的数据库

发布于 2024-09-03 11:45:03 字数 971 浏览 3 评论 0原文

如果我的 MessageHandler 中发生异常,我想将异常详细信息写入我的数据库。 我该怎么做?

显然,我不能只是捕获异常,写入数据库,然后重新抛出它,因为 NSB 回滚了所有更改。 (IsTransactional 设置为 true)

我尝试在单独的处理程序中添加日志记录功能,如果发生异常,我会使用 SendLocal 进行调用,但这不起作用:

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

我还尝试使用带有自定义附加程序的 Log4Net,但这也回滚了。

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

附加程序:

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);
    }
}

当 IsTransactional 为 true 时,是否可以在消息处理程序中记录未处理的异常?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小糖芽 2024-09-10 11:45:03

我建议使用配置文件而不是代码来配置附加程序。这样,无论谁操作您的软件,都可以决定如何记录内容。标准 AdoNetAppender 具有以下配置开关:

<param name="UseTransactions" value="False" />

我认为这会按照您想要的方式工作。如果您确实想使用自己的附加程序,您可以查看 log4net 源代码,了解他们是如何做到这一点的。他们可能将 TransactionScope 类与 TransactionScopeOption.Suppress 选项

I recommend to configure appenders with a configuration file and not in code. This way whoever operates your software can decide how stuff should be logged. The standard AdoNetAppender has the following configuration switch:

<param name="UseTransactions" value="False" />

I think this would work the way you want. If you really want to use your own appender you can either check in log4net source code how they do it. They probably use the TransactionScope class with the TransactionScopeOption.Suppress option.

笑红尘 2024-09-10 11:45:03

NServiceBus 已经记录了处理程序和 saga 异常 OOTB。这些日志条目可以配置为记录到任何常见的日志库

您还可以订阅错误通知并以编程方式处理它们

NServiceBus already logs handler and saga exceptions OOTB. Those log entries can be configured to log to any of the common logging libraries

You can also subscribe to error notifications and handle them programmatic

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文