System.Diagnostics.Trace - correct way to log exceptions

daramasala

I'm using the Trace class from an Azure Worker Role.

I want to log exceptions in a way that will print all the information about the exception which is usually:

  • The exception message
  • Exception stacktrace
  • Recursively print the inner exceptions

I only see a Trace.TraceError method that gets a string and arguments. Isn't there something equivalent to Java's logging frameworks that gets an exception and know how to log it? (yes, I am doing my first steps in MS world...)

Erno

No, there isn't. But you could write an extension method for the Exception class that does this so you could call

someException.Trace();

The ToString method of the Exception class probably returns all you want so just Trace that. You could add additional information (recurse inner exceptions if the stack trace isn't sufficient) process id, user id, thread id, and such in that same method.

public static class ExceptionExtensions
{
    public static void Trace(this Exception _this)
    {
        Trace.TraceError("{0:HH:mm:ss.fff} Exception {1}", DateTime.Now, _this);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

System.Diagnostics.Tracing.EventSource vs System.Diagnostics.Trace

System.Diagnostics.Trace vs. ILogger

System.Diagnostics.Trace class in notepad++

What's the difference between System.Diagnostics.Trace, System.Diagnostics.Debug and System.Console?

System.Diagnostics.Trace or System.Diagnostics.WriteLine and CRM 2011 Plugin

Application Insights - System.Diagnostics.Trace.* - What are these used for?

Does System.Diagnostics.Trace have any performance degredation?

Why would I turn System.Diagnostics trace autoflush off?

Correct way to log exceptions using log4j and Solarlint compliant

Is there a way to use System.Diagnostics.Process in an IAsyncEnumerator?

Correct way of throwing exceptions with Reactor

Correct way to handle exceptions in Java

System.Diagnostics.EventLog doesn't contain correct message

How is Visual Studio or Rider reading messages written using System.Diagnostics.Trace.Write?

Correct way to handle exceptions in Spring Boot

Python: Correct way to handle chain exceptions

Correct way to document bubbled exceptions in C#?

The correct way to log, and where to log, in PHP

How to enable log-back log stack trace og uncaught exceptions

Secure way to log PHP exception trace (that might include sensible credentials)

A correct/common way to log async calls

Correct way to log events in another layer on ServiceStack

Is this the correct way to log to a specific target with NLog?

The correct way to ignore log entries with NLog at runtime

What is the best way to correct parse log file?

Is it correct way to log the open connections using CachingClientConnectionFactory

Trace messages from Website not appearing in Azure Diagnostics

Diagnostics, where to read the Trace.TraceInformation?

How do you use System.Diagnostics.TextWriterTraceListener to log to a file in C#? Are my configuration files broken?