UnhandledExceptionLogger 2.1.0

dotnet add package UnhandledExceptionLogger --version 2.1.0                
NuGet\Install-Package UnhandledExceptionLogger -Version 2.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="UnhandledExceptionLogger" Version="2.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add UnhandledExceptionLogger --version 2.1.0                
#r "nuget: UnhandledExceptionLogger, 2.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install UnhandledExceptionLogger as a Cake Addin
#addin nuget:?package=UnhandledExceptionLogger&version=2.1.0

// Install UnhandledExceptionLogger as a Cake Tool
#tool nuget:?package=UnhandledExceptionLogger&version=2.1.0                

UnhandledExceptionLogger

A simplistic logger that hooks up to unhandled exceptions and logs them to a specified CSV file.

Installation

To install ErrorLogger, simply add the NuGet package to your project.

Usage

To start using ErrorLogger, initialize the logger in your main method:

var logger = new Logger();

You can also specify the log file path, the duration of the log history to keep, and the maximum size of the log file in kilobytes (KB) by using the following code:

var logger = new Logger(filePath, historyDuration, maxLogSize_KB,logLevel,debugOutLevel);

The logger should then pick up eceptions automatically. Note there are some known issues when working with multiple threads/tasks/contexts to pick up the exceptions.

Furthermore you can log exceptions and messages manually (for example for debugging or to log handeled exceptions):

try
{
    // Some code that may throw an exception...
    int result = int.Parse("not a number");
}
catch (Exception ex)
{
    logger.Log(ex, Severity.Critical);
}
logger.Log("Application Start", "The application started successfully.", Severity.Info);

Example

Here's an example of how you can use ErrorLogger in your project:

using ErrorLogger;

namespace MyProject
{
    class Program
    {
        static void Main(string[] args)
        {
            // initialize logger
            var logger = new Logger(
                filePath: "C:\\logs\\error.csv", 
                historyDuration: TimeSpan.FromDays(30.0), 
                maxLogSize_KB: 1024
                );
            // unhandled exception occurs, application crashes but exception gets logged
            throw new Exception("Test exception");
        }
    }
}

In the example above, the logger is initialized with the file path "C:\logs\error.csv", a history duration of 30 days, and a maximum log size of 1024 KB. The log file is roll over, old entries older than the specified duration are deleted. The same is true for the logfile size.

If no parameters are specified, the logger loggs to the application directory into a file errors.log with infinite size.

Note that there can be issues in multithreaded applications for the logger to pick up the exceptions.

sample output

Date and Time Severity Exception Type Description Location
2/26/2023 21:57 Critical DataMisalignedException data is incorrect! at Program.<Main>$(String[] args) in C:\Users\julia\OneDrive\Projects\Libraries\ErrorLogger\TestingApplication\Program.cs:line 9
2/26/2023 21:58 Critical Exception this is a test exeption! at Program.<Main>$(String[] args) in C:\Users\julia\OneDrive\Projects\Libraries\ErrorLogger\TestingApplication\Program.cs:line 9
2/26/2023 21:58 Critical DataMisalignedException the data is misaligned! at Program.<Main>$(String[] args) in C:\Users\julia\OneDrive\Projects\Libraries\ErrorLogger\TestingApplication\Program.cs:line 9

License

ErrorLogger is released under the MIT-MODERN-VARIANT license.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.0 92 11/13/2024
2.0.0 137 3/29/2024
1.1.1 202 7/26/2023
1.1.0 171 7/12/2023
1.0.1 215 2/26/2023
1.0.0 203 2/26/2023

2.0.0
- improved performance in multithreaded scenarios
- updated tests
- added .Net 8

1.1.0
- added functionality of loglevels
- updated naming to make more sense
- added capability to log a message