Easy.Log.Writer 2.0.14

dotnet add package Easy.Log.Writer --version 2.0.14                
NuGet\Install-Package Easy.Log.Writer -Version 2.0.14                
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="Easy.Log.Writer" Version="2.0.14" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Easy.Log.Writer --version 2.0.14                
#r "nuget: Easy.Log.Writer, 2.0.14"                
#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 Easy.Log.Writer as a Cake Addin
#addin nuget:?package=Easy.Log.Writer&version=2.0.14

// Install Easy.Log.Writer as a Cake Tool
#tool nuget:?package=Easy.Log.Writer&version=2.0.14                

Easy Logger

MIT NuGet

This project was created to provide an easy to use and configurable logging library. If the default configuration is sufficient for your needs the library can be used out of the box without further setup. However, if you have specific logging needs you can alter the library configuration settings and also provide custom logging methods.

The default implementation is capable of:

  • Recording to text log files
  • Recording to JSON log files
  • Recording to an in-memory dictionary
  • Recording to the console
  • Recording to the web console (requires use of the Easy.Log.Writer.Blazor package or Easy-Logger-Blazor.csproj project for coloring and formatting log messages)
  • Recording to a SQL Server (requires use of the Easy.Log.Writer.Sql package or Easy-Logger-Sql.csproj project)
  • Adding dated folders to text-based logs (ex. /logs/2020/05/01/log.txt)
  • Adding templated filenames to text-based logs (ex. /logs/2020-05-01_My.Namespace_Log_150059.txt)
  • Being used as an ILogger implementation for ASP.NET and other API type applications
  • Parsing and searching JSON log files (requires downloading and installing the Easy Logger Parser app)

Breaking Changes V2.0

Notice!

If you have been using V1 based editions of this library, please use the Version 1.X branch to ensure you do not encounter breaking changes. It is no longer being developed, but if there are security concerns they will be addressed.

The V2 edition has been released to provide a more standards based logging system. The loggers and providers are now using the default ILogger and ILoggerProvider implementations. It is recommended to register your required loggers using the providers and then using dependency injection to get ILogger instances as required. You can still directly create loggers if required.

Getting Started

These instuctions can be used to acquire and implement the library.

Installation

To use this library:

Usage

Recommended Example

The following provides an example of the recommended usage.

In this example both the text logger and console loggers are configured, so any log messages will be recorded to both. The text logger is configured using a custom formatter that will be applied as logs are saved to the text files; whereas the console logger has not been configured using a custom formatter, so the default formatter will be used.

Program.cs or configuration class

var builder = WebApplication.CreateBuilder();

var logLevels = new[] { LogLevel.Trace, LogLevel.Debug, LogLevel.Information, LogLevel.Warning, LogLevel.Error, LogLevel.Critical };
var ignoredMessages = new List<string>() { "TaskCanceledException" };

builder.Logging
    .ClearProviders()
    .AddTextLogger(options =>
    {
        options.LogLevels = logLevels;
        options.IgnoredMessages = ignoredMessages;
        options.LogDirectory = Path.Combine(AppContext.BaseDirectory, "Logs");
        options.LogfileNameTemplate = "[Date:yyyy]-myapplog-[Date:MM-dd_HH]";
        options.SubdirectoryMode = DatedSubdirectoryModes.Daily;

        options.Formatter = entry =>
        {
            return $"{entry.Timestamp:yyyy-MM-dd HH:mm:ss.fff}; Severity={entry.Severity}; Source={entry.Source}; Text={entry.Message}";
        };
    })
    .AddConsoleLogger(options => 
    {
        options.LogLevels = logLevels;
        options.IgnoredMessages = ignoredMessages;

        options.LogLevelToColorMap = new Dictionary<LogLevel, ConsoleColor>()
        {
            [LogLevel.Trace] = ConsoleColor.Cyan,
            [LogLevel.Debug] = ConsoleColor.Blue,
            [LogLevel.Information] = ConsoleColor.Green,
            [LogLevel.Warning] = ConsoleColor.Yellow,
            [LogLevel.Error] = ConsoleColor.Red,
            [LogLevel.Critical] = ConsoleColor.Magenta
        };
    });

Controller or other service

public sealed class InfoController : ControllerBase 
{
    private readonly ILogger logger;

    public InfoController(ILogger<InfoController> logger) 
    {
        this.logger = logger;
    }

    [HttpGet]
    [ProducesResponseType(typeof(IApiResponse<bool>), StatusCodes.Status200OK)]
    public IActionResult Test() 
    {
        logger.LogInformation($"{nameof(Test)} function queried.");
        return Ok(true);
    }
}

Per-Message Formatting Example

The following provides an example of customizing the formatting for a single message. The per-message formatting is applied to the ILoggerEntry.Message property; if the configuration for the logger has a global formatter, that will be applied afterwards.

public void Test(string? message)
{
    try 
    {
        if (string.IsNullOrWhitespace(message))
            throw new ArgumentException("must provide a message", nameof(message));

        logger.Log(LogLevel.Information, new EventId(), message, null, (state, exception) => $"{nameof(Test)} function succeeded with message {state}.");
    }
    catch (Exception e)
    {
        logger.Log<object?>(LogLevel.Error, new EventId(12345), null, e, (state, exception) => $"{nameof(Test)} function failed with {exception.GetType().Name} {exception.Message}.");
    }
}

Direct Example

The following provides an example of using a logger directly.

Console.WriteLine("Enter a message to log:");
var message = Console.ReadLine();

var logger = new ConsoleLogger("", () => new LogLevel[] { LogLevel.Information, LogLevel.Warning }, new List<string>() { "TaskCanceledException" }, new Dictionary<LogLevel, ConsoleColor>());
logger.LogInformation(input);

Blazor WebAssembly Example

The following example displays how the memory logger can be used to download a file in Blazor WebAssembly. This method is also useful in other situations where file system access is either unavailable or restricted. The example provides a button that users can click to save the logs; this has proven useful over console logging as the user can save logs then navigate elsewhere.

Program.cs or configuration class

var builder = WebAssemblyHostBuilder.CreateDefault(args);

builder.Logging.AddMemoryLogger(x =>
{
	x.LogLevels = new[] { LogLevel.Information, LogLevel.Warning, LogLevel.Error, LogLevel.Critical };
});

Blazor Component

<button type="button" @onclick=OnClickSaveReport>Save Logs</button>

@code {
    [Inject]
	private IJSRuntime JsRuntime { get; init; }

    private async void OnClickSaveReport()
	{
		// Generate file content
		var text = JsonSerializer.Serialize(MemoryLoggerProvider.DefaultLogger.Select(x => x.Value).OrderBy(x => x.Timestamp));
		var fileName = $"{DateTime.Now:yyyyMMdd_HHmmss}_debug.txt";

		// Stream
		var bytes = Encoding.ASCII.GetBytes(text);
		var fileStream = new MemoryStream(bytes);

		using var streamRef = new DotNetStreamReference(fileStream);
		await JsRuntime.InvokeVoidAsync("DownloadFileFromStream", fileName, streamRef);
	}
}

JavaScript Function

/**
 * Converts the provided stream to a blob in memory and downloads the file
 * @param {string} fileName The name to assign the downloaded file
 * @param {any} contentStreamReference A reference to the steam to download
 */
DownloadFileFromStream: async function (fileName, contentStreamReference) {
    var arrayBuffer = await contentStreamReference.arrayBuffer();
    var blob = new Blob([arrayBuffer]);
    var url = URL.createObjectURL(blob);

    this.DownloadFileFromUrl(fileName, url);

    URL.revokeObjectURL(url);
}

Blazor Browser Console Example

The following example displays how to configure the console logger in a web browser. This uses the Blazor NuGet package.

var builder = WebAssemblyHostBuilder.CreateDefault(args);

builder.Logging.AddBrowserLogger(x =>
{
	x.LogLevels = new[] { LogLevel.Information, LogLevel.Warning, LogLevel.Error, LogLevel.Critical };
});

Authors

  • NF Software Inc.

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgments

Thank you to:

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Easy.Log.Writer:

Package Downloads
Easy.Log.Writer.Blazor

This project was created to provide an extension to the Easy.Log.Writer package that includes a Blazor logging endpoint. The base package can also output to the web console, but does not have advanced features such as custom log color or formats.

Easy.Log.Writer.Sql

This project was created to provide an extension to the Easy.Log.Writer package that includes a SQL Server logging endpoint.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.14 108 12/19/2024
2.0.13 234 11/11/2024
2.0.12 389 7/10/2024
2.0.11 173 5/20/2024
2.0.10 134 5/7/2024
2.0.9 286 3/21/2024
2.0.6 383 12/15/2023
2.0.5 172 11/27/2023
2.0.4 154 11/2/2023
2.0.3 211 8/24/2023
2.0.2 182 7/24/2023
2.0.1 182 4/27/2023
1.0.5 105 2/15/2024

2.0.14
Add new constructors to logging providers to enable usage without DI

2.0.13
Update requirement for System.Text.Json to patch vulnerability

2.0.12
Update requirement for System.Text.Json to patch vulnerability

2.0.11
Add trailing comma in dirty JSON mode

2.0.10
Add dirty JSON mode

2.0.9
Update publishing to include source link, deterministic, and compiler flags

2.0.6
Minor performance improvement

2.0.5
Update NuGet requirements to enable use with .NET 7 and .NET 8

2.0.4
Fix issue causing console logger to fail in WebAssembly projects

2.0.3
Update package references to use floating versions

2.0.2
Update project file to generate XML documentation in NuGet package

2.0.1
Bug fixes

2.0.0
Add logger providers, Add Memory Logger, Add Console Logger, Remove SQL logger, Remove obsolete interfaces, Update loggers to implement ILogger

1.0.4 Update NuGet packages to address security vulnerability in System.Data.SqlClient
1.0.3 Add XML file to output to allow Intellisense to work with package, Update dependent NuGet packages
1.0.2 Fix issue causing logger to fail on Linux deployments
1.0.1 Add ASP.NET compatible log provider