structured.logs 0.0.2

dotnet add package structured.logs --version 0.0.2
                    
NuGet\Install-Package structured.logs -Version 0.0.2
                    
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="structured.logs" Version="0.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="structured.logs" Version="0.0.2" />
                    
Directory.Packages.props
<PackageReference Include="structured.logs" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add structured.logs --version 0.0.2
                    
#r "nuget: structured.logs, 0.0.2"
                    
#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.
#:package structured.logs@0.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=structured.logs&version=0.0.2
                    
Install as a Cake Addin
#tool nuget:?package=structured.logs&version=0.0.2
                    
Install as a Cake Tool

BioIntellisense Structure Logs Packages

Package to share and standardize all the configurations to structured the logs as JSON used in different C# and DotNet components.

Created: 2024-05-10 Author: Marcelo Marques

License Apache License Version 2.0

© 2024 Biointellisense, Inc

Prerequisites

AWS Account

  • You will need an active AWS account that we can use to push logs from our .NET application to AWS CloudWatch.
  • Please note that you should always make sure to give applications their own IAM roles/users in production with minimal permissions according to AWS best practices.

How to use

Minimal use case:

Add the Bio StructuredLogs package and set up a logger

  • First add The structured.logs and AWS SDK CloudWatch packages from NuGet
  • Next, on your Program.cs add:
using NLog;
using LogLevel = NLog.LogLevel;
using structured.logs;


var log = new StructuredLogs();
log.ConfigureNLog(logAwsLevel: LogLevel.Info);
Logger logger = LogManager.GetCurrentClassLogger();

// From here you can now start adding any logging you need:
logger.Info("Start the ToDolist Web App"); 
  • Then, in each class, you need to log something, add:

    • Similar to what you do with context, create a class field for the logger:

      private readonly Logger _logger;

    • injecting the dependencies into constructor of the class by call the get the current class logger, it will get the logger with all the configurations you were set at the Program.cs:

      _logger = LogManager.GetCurrentClassLogger();

    Note: Alternatively, you can simplify these two steps into just one assignment of the GetCurrentClassLogger directly to the class field.

    private readonly Logger _logger = LogManager.GetCurrentClassLogger();

After this configuration, you can use this Logger to log what you need from this class, like:

_logger.Warn( "Id {Id} informed is different from the task.", id);

More Instructions Details of Different Configurations Kinds Supported:

Console as log target:

To log into the console with a minimum level of debugging:

log.ConfigureNLog();

Note:

  • In that case, it default log level is Debug, you can change it by passing the parameter 'logConsoleLevel' with your level needs.
  • Depending on your C#/DotNET application deployment on AWS, you may only need to configure logging for the console as it can also be captured by AWS logging.
  • If you want to log on to the console and simultaneously to another target, you must provide the 'logConsoleLevel' parameter, even if you want to maintain the debug default for the console.
File as log target:

To log into a file:

log.ConfigureNLog(logFileLevel: LogLevel.Info);

In this case, you can customise the file logging configuration by setting:

  • logFileLevel: The minimum log level needed to trigger the file logging.
  • logFileName: Sets the name of the log file to write to. Default as "Bio.App"
  • archiveAboveSize: Sets the size in bytes above which log files will be automatically archived. Default as 5000000 (5 Mb)

Note: The file logging is only set and added if the logFileLevel parameter was informed.

AWS as log target:

To log directly into AWS:

log.ConfigureNLog(logAwsLevel: LogLevel.Info);

  • logAwsLevel: The minimum log level needed to trigger the AWS logging.

Note: The AWS logging is only set and added if the logAwsLevel parameter was informed.

Configure More Than one Target:

You can set more than one target too, as for eg:

log.ConfigureNLog(logConsoleLevel: LogLevel.Debug, logFileLevel: LogLevel.Info, logAwsLevel: LogLevel.Info);

General configuration affects all targets:

For all logger is that possible set a different JSON log layout by set the jsonLayout, a specialized layout that renders to JSON. Click here to see the configuration parameters details

See below the log levels ordered by severity:

  • Trace (Ordinal = 0) : Most verbose level. Used for development and seldom enabled in production.
  • Debug (Ordinal = 1) : Debugging the application behavior from internal events of interest.
  • Info (Ordinal = 2) : Information that highlights progress or application lifetime events.
  • Warn (Ordinal = 3) : Warnings about validation issues or temporary failures that can be recovered.
  • Error (Ordinal = 4) : Errors where functionality has failed or Exception have been caught.
  • Fatal (Ordinal = 5) : Most critical level. Application is about to abort.

References:

If you would like to find out more about NLog then you can view their documentation here.

You can also find out more about the AWS Target for NLog here.

Product Compatible and additional computed target framework versions.
.NET 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
0.0.2 176 5/14/2024