Building an exception in C #

I inherited a code base that uses a compiled logging library. I cannot update the logging library. This library has a method that registers details for an exception. The method takes one exception as a parameter. Now I am creating a mobile application that will be attached to this system.

In this mobile application, I have a block of code that handles uncaught exceptions. I need to register them on the server. But now I can transfer data over the network in a lowercase format. Because of this, I have a service that accepts an error message, a stack trace and different lines. I need to take these lines and convert them to Exception so that I can pass them to my preexisting library.

How can I take a message and stackTrace as strings and bind them to Exception? The task here is Message and StackTrace are read-only.

Thanks!

+4
source share
2 answers

StackTrace is virtual, so you can define your own Exception like this:

 public class MyException : Exception { private readonly string stackTrace; public override string StackTrace { get { return this.stackTrace; } } public MyException(string message, string stackTrace) : base(message) { this.stackTrace = stackTrace; } } 

and then pass instances of MyException into your registration code. This gives you complete control over the Message and StackTrace .

+7
source

Exceptions must be serialized, so you can try to serialize the data. Then you can de-serialize it later and you should have the same exception.

I think SoapFormatter should allow you to send it over the network, or at least give a string representation.

+1
source

Source: https://habr.com/ru/post/1337781/


All Articles