简体   繁体   English

C#:将类型化的异常发送到另一个进程->转换为基本异常?

[英]C#: Sending typed exceptions to another process -> transform to base exception?

I'm working on a distributed system composed of multiple services (processes) working together orchestrated via message broker (service bus). 我正在一个由多个服务(进程)组成的分布式系统上,这些服务通过消息代理(服务总线)协同工作。 Some of these service are very time sensitive, so we had the idea of using the bus the propagate exceptions to a service that would be used as a central point to log (to a database/on disk) the exceptions of all other services in order not to create latency in our services because we know sending a message is faster than writing on disk. 其中一些服务对时间非常敏感,因此我们想到了使用总线将异常传播到服务的方法,该服务将被用作中心点,以便按顺序记录(到数据库/磁盘上)所有其他服务的异常不要在我们的服务中造成延迟,因为我们知道发送消息比在磁盘上写入要快。 So using NLog, I create a new target for sending exceptions on bus and redirect all service exception handling to it. 因此,使用NLog,我创建了一个用于在总线上发送异常的新目标,并将所有服务异常处理重定向到该目标。

It works fine for basic framework exceptions, but of course some exceptions can be pretty complex objects. 对于基本框架异常,它工作正常,但是当然,某些异常可能是非常复杂的对象。 When using different libraries, not only are some exception not serializable (ex: IronPython), but more importantly our ExceptionLogger service don't have reference to every external dll used by every other endpoints, so it can't instantiate the typed exception. 当使用不同的库时,不仅某些异常无法序列化(例如IronPython),更重要的是我们的ExceptionLogger服务没有引用其他所有端点使用的每个外部dll,因此无法实例化类型化的异常。

Here's a few idea that I had to fix my problem, but none work: 这里有一些想法我必须解决我的问题,但是没有用:

  1. Have the exception logger service reference every dlls used by the other services. 让异常记录器服务引用其他服务使用的每个dll。 In my opinion this is not clean, pretty overkill and still don't handle the case of exceptions that can't even be serialized and passed on the bus. 在我看来,这是不干净的,相当大的杀伤力,并且仍然无法处理甚至无法序列化并在总线上传递的异常情况。

  2. Creating a custom class that doesn't inherit from exception to pass only the data (as Antony Booth method here: How to serialize an Exception object in C#? ). 创建一个不继承自异常的自定义类,以仅传递数据(如此处的Antony Booth方法: 如何在C#中序列化Exception对象? )。 Problem with this is that when I receive an instance of this class on my exception logger endpoint and want to trigger a log, I can't create a new Exception() from it to create a NLog LogEventInfo object. 问题是,当我在异常记录器端点上收到此类的实例并想要触发日志时,无法从中创建新的Exception()来创建NLog LogEventInfo对象。

  3. Transform the specially typed exception to a base native Exception type before sending it on the bus. 在总线上发送特殊类型的异常之前,请将其转换为基本的本地异常类型。 This way, I lose some information, but I would keep the Message, Stacktrace and the InnerException stack, which is pretty much everything I need. 这样,我会丢失一些信息,但是会保留Message,Stacktrace和InnerException堆栈,这几乎是我需要的所有东西。 Unfortunately, I did not find a way to do it and don't think it is possible. 不幸的是,我没有找到一种方法,也不认为有可能。

That makes me question the relevance of the whole idea, am I simply going into a wrong path? 这使我怀疑整个想法的相关性,我只是在走错路了吗?

Option 4. Create an Exception assembly to store all of the custom exception types within your organization and reference that assembly in every other service. 选项4.创建一个Exception程序集以存储组织内所有自定义异常类型,并在所有其他服务中引用该程序集。 It's a centralized store of Exceptions. 它是异常的集中存储。

[Serializable]
public sealed class SerializableException : Exception
{
    public SerializableException()
    {
    }

    public SerializableException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }

    public SerializableException(Exception exception)
        : base(exception.Message, exception.InnerException == null ? null : new SerializableException(exception.InnerException))
    {
        _data = exception.Data;
        HelpLink = exception.HelpLink;
        HResult = exception.HResult;
        Source = exception.Source;
        _stackTrace = exception.StackTrace;
    }

    public override IDictionary Data { get { return _data; } }
    public override string StackTrace { get { return _stackTrace; } }

    private readonly IDictionary _data;
    private readonly string _stackTrace;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM