using MyLib.Logging; // package library public class MyClass { private ITracer tracer = LogManager.GetTracer("MyApp.MyClass"); public void MyMethod() { try { // some code that may throw an exception } catch (Exception ex) { tracer.Error("An error occurred in MyMethod", ex); throw; // re-throw the exception } } }
using NLog; // package library public class MyClass { private ILogger logger = LogManager.GetCurrentClassLogger(); public void MyMethod() { try { // some code that may throw an exception } catch (Exception ex) { logger.Error(ex, "An error occurred in MyMethod"); throw; // re-throw the exception } } }In this example, we are using the NLog library (package library) to get a logger instance for our class. We then use the logger object to log an error with the exception and a message. We then re-throw the exception so that it can be handled by higher-level code.