Spa.Cbcrc.Logging
1.0.4
See the version list below for details.
dotnet add package Spa.Cbcrc.Logging --version 1.0.4
NuGet\Install-Package Spa.Cbcrc.Logging -Version 1.0.4
<PackageReference Include="Spa.Cbcrc.Logging" Version="1.0.4" />
paket add Spa.Cbcrc.Logging --version 1.0.4
#r "nuget: Spa.Cbcrc.Logging, 1.0.4"
// Install Spa.Cbcrc.Logging as a Cake Addin
#addin nuget:?package=Spa.Cbcrc.Logging&version=1.0.4
// Install Spa.Cbcrc.Logging as a Cake Tool
#tool nuget:?package=Spa.Cbcrc.Logging&version=1.0.4
SPA.Cbcrc.Logging
SPA.Cbcrc.Logging is the official package for the Software and Platform Architecture team's logging solution for Merlin Template driven applications. The package leverages Serilog and supports logging to Files as well as to Azure Log Analytics.
Table of Contents
Installation
Chose one of the following way to install the package:
Use Visual Studio's Package Manager Console and search for SPA.Cbcrc.Logging
Navigate to your project's directory and use the following command:
dotnet add package SPA.Cbcrc.Logging
Usage
SPA.Cbcrc.Logging leverages dependency injection to provide the logging methods to your classes. The service needs to be registered in the Program.cs of all of your relevant projects before being usable.
Configuration
{
"ApplicationId": "[Application Name]",
"Serilog": {
"MinimumLevel": {
"Default": "Warning",
"Override": {
"Microsoft": "Error",
"System": "Error"
}
},
"FileSink": {
"OutputTemplate": "{Timestamp} [{Level}] V{Release} (#{ProcessId}-{ThreadId}) {Message}{NewLine}{Exception}",
"Path": "c:\\Logs\\[Application Name]\\log-api-.txt",
"RollingInterval": "Day",
"MinimumLevel": "Information"
},
"AzureLogAnalytics": {
"MinimumLevel": "Information",
"WorkspaceId": "d953bda0-1a70-4c32-a98e-9434bd6d8b78",
"PrimaryKey": "@---ReplaceInCICD-AzureLogAnalyticsKey---@"
}
},
}
Property | Description | Required |
---|---|---|
ApplicationId | Short name of the application that will appear in Azure Log Analytics (the name will be transformed automatically into PascaleCase). Ex.: merlin-template | ✔ |
Serilog.MinimumLevel.Default | The minimum logging level for all logging methods (File Sink and Azure Log Analytics). This setting will override the finer grained minimum levels. Allowed values: Verbose, Fatal, Error, Warning, Information. | ✔ |
Serilog.MinimumLevel.Override | Overrides the .Net namespace minimum logging levels. Allowed values: Verbose, Fatal, Error, Warning, Information. | |
Serilog.FileSink.OutputTemplate | For File Sink logging: The format of the log events. | ✔ |
Serilog.FileSink.Path | For File Sink logging: The path where the log file will be saved. | ✔ |
Serilog.FileSink.RollingInterval | For File Sink logging: The frequency at which the log file is overwritten with new logs. Allowed values: Minute, Hour, Day, Month, Year, Infinite. | ✔ |
Serilog.FileSink.MinimumLevel | For File Sink logging: The minimum logging level. Allowed values: Verbose, Fatal, Error, Warning, Information. | ✔ |
Serilog.AzureLogAnalytics | Section for Azure Log Analytics settings. The section is not mandatory and can be left missing for local development. If you wish to to log to Azure Log Analytics in local development, please do not commit secrets to your repository. For Local development: you can use the .Net Secret Manager | |
Serilog.AzureLogAnalytics.MinimumLevel | For Azure Log Analytics logging: The minimum logging level. Allowed values: Verbose, Fatal, Error, Warning, Information. | ✔ |
Serilog.AzureLogAnalytics.WorkspaceId | For Azure Log Analytics logging: The Azure Log Analytics GUID: <br/> Business <br/> Nonprod: c664d046-45fb-4a81-9f15-639338e15607 <br/> Prod: c0336ed8-467b-44f9-8c3d-27ba144cc822 <br/> Content <br/> Nonprod: 47cb679d-fb74-446d-b757-fd4e22ec7f5a <br/> Prod: d9e1180c-c2a2-4d65-84ae-935097f29d50 <br/> Media <br/> Nonprod: 0d03b14e-9f7e-4115-b8be-a9c62e8b3d67 <br/> Prod: 0d201065-173c-4ff0-911f-1bf9e9d8d886 | ✔ |
Serilog.AzureLogAnalytics.PrimaryKey | For Azure Log Analytics logging: The transformation string for the Azure Log Analytics secret Primary Key. Set it to: "@---ReplaceInCICD-AzureLogAnalyticsKey---@" | ✔ |
SPA.Cbcrc.Logging classes and interfaces
Spa.Cbcrc.Logging <br/> Interface used to define the type of the injected Logging service.
Spa.Cbcrc.Logging.Settings <br/> This class defines the AppSetting properties and is used to construct an Options pattern settings class for your project. Needs to be used to bind to IConfigurationBuilder in the case of console application.
Registering the Logging Service
WebApplicationBuilder (API and Web Application)
For projects that use WebApplicationBuilder, like APIs and Web Applications, simply register the service in the Program.cs class (or Startup.cs for .Net Core). <br/><br/> Program.cs
builder.Services.AddCbcrcLogger();
<br/>
WebApplicationBuilder (Windows Service and Console Applications)
For Console Application projects, like Windows Services, an IConfigurationRoot object must be instantiated from the Configuration Builder and bound to an options object that contain the SerilogOptions class. Then, using these settings, instantiate the CbcrcLogger class in the Program.cs class (or Startup.cs for .Net Core). <br/><br/> Program.cs
// Instantiate appSettings object for configuration IConfigurationRoot appSettings = new ServiceAppSettingsOptions(); configuration.Bind(appSettings); //Configure Serilog _logger = new CbcrcLogger(appSettings.ApplicationId, appSettings.Serilog);
<br/>
Using the Logging Service
Simply declare an ICbcrcLogger variable in your class and assign the injected services from the class constructor to the variable like so:
public class MyController : ApiBaseController
{
private readonly ICbcrcLogger _logger;
public MyController(ICbcrcLogger logger)
{
_logger = logger;
}
}
<br/> Please note that for Console Application like Windows services, the Logging service must be passed as a parameter to the different classes that require it.
var serviceName = appSettings.Service.Name;
var serviceInterval = appSettings.Service.IntervalMs;
_logger = new CbcrcLogger(appSettings.ApplicationId, appSettings.Serilog);
new Worker(serviceInterval, serviceName, _logger);
The ICbcrcLogger service can then be used with it's different methods that refer to all of the available logging levels. For example:
catch(Exception ex)
{
_logger.Error("MyController", ex);
}
<br/>
Here are the available logging methods. Note that the non mandatory "assemblyName" parameter is used to enable or disable adding the project name to the Scope property of the log events.
public interface ICbcrcLogger
{
void Verbose(string scope, string msg, string email = "", bool assemblyName = true);
void Info(string scope, string msg, string email = "", bool assemblyName = true);
void Warning(string scope, string msg, string email = "", bool assemblyName = true);
void Error(string scope, Exception ex, string email = "", bool assemblyName = true);
void Fatal(string subFilter, Exception ex, string email = "", bool assemblyName = true);
}
Sample Code
The Merlin Template repository integrates the SPA.Cbcrc.Logging package. <br/> it-apps-merlin-template
Contributing
Pull requests are welcome. For major changes, please contact the SPA team to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
Product | Versions 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 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. |
-
net6.0
- Microsoft.Extensions.Configuration.UserSecrets (>= 6.0.1)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 6.0.0)
- Microsoft.Extensions.Options.DataAnnotations (>= 6.0.0)
- Serilog.Enrichers.Environment (>= 2.2.0)
- Serilog.Enrichers.Process (>= 2.0.2)
- Serilog.Enrichers.Thread (>= 3.1.0)
- Serilog.Sinks.AzureAnalytics (>= 4.8.0)
- Serilog.Sinks.File (>= 5.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.