public void MyMethod() { Trace.WriteLine("MyMethod was called."); // more code... }
try { // some code that might throw an exception } catch (Exception ex) { Trace.TraceError("An error occurred: {0}", ex.Message); }In this example, Trace.TraceError() is used to log an error message to the console or log file when an exception is caught. Both of these examples use the Trace class from the System.Diagnostics namespace, which is part of the .NET framework. Therefore, no external package or library is required.