using NLog; class Program { static void Main(string[] args) { var logger = LogManager.GetCurrentClassLogger(); try { // Some code that may throw an exception } catch (Exception ex) { logger.Fatal(ex, "An unrecoverable error occurred. The application will now exit."); Environment.Exit(1); } } }
using NLog; class MyService { private readonly ILogger _logger; public MyService() { _logger = LogManager.GetCurrentClassLogger(); } public void Start() { // Some code to start the service _logger.Info("MyService started."); } public void Stop() { // Some code to stop the service _logger.Info("MyService stopped."); _logger.Fatal("An unrecoverable error occurred. The service will now stop."); } }This example shows how to use NLog to log fatal errors in a service application. The logger is instantiated in the constructor and used to log information messages when the service is started and stopped. When a fatal error occurs, the logger writes a message and the service stops. Package library: NLog