C# – Global exception event handlers

There are two global exception event available in all .NET applications: FirstChanceException: When any exception is thrown, this event is fired before anything else. UnhandledException: When there’s an unhandled exception, this event is fired right before the process is terminated. You wire up these event handlers in Main() (before anything else has executed), like this: … Read more

How to use NLog in ASP.NET

When you want to use NLog in ASP.NET, the first step is to install and configure NLog. Then you can either use NLog directly or fully integrate NLog and ASP.NET. Use NLog directly if you prefer to have static ILogger properties, instead of using dependency injection. The downside of this approach is that you’ll have … Read more

NLog – Log to console

There are two configuration options for logging to the console using NLog: Console target – uses the default text colors. ColoredConsole target – allows you to configure the color of logging messages. In this article, I’ll show how to configure these two targets using nlog.config. At the end, I’ll show an example of configuring NLog … Read more

NLog – Archive by file size

To archive by file size when you’re using NLog, you can configure nlog.config like this: You specify archiveAboveSize in bytes. The above configuration is specifying ~1 MB. When your log file hits the specified size, NLog will “archive” the file, which really just means it will rename the log file and start a new log … Read more