SLogger 1.0.2
dotnet add package SLogger --version 1.0.2
NuGet\Install-Package SLogger -Version 1.0.2
<PackageReference Include="SLogger" Version="1.0.2" />
<PackageVersion Include="SLogger" Version="1.0.2" />
<PackageReference Include="SLogger" />
paket add SLogger --version 1.0.2
#r "nuget: SLogger, 1.0.2"
#:package SLogger@1.0.2
#addin nuget:?package=SLogger&version=1.0.2
#tool nuget:?package=SLogger&version=1.0.2
SLogger (a SeriLog implementation)
This is an implemention of SeriLog using DotNet Core and tweaked for EF Core
- version dotnet core 3.1
SETUP
Installation of NuGet package
- at this use a the custom store where the nuget package is located
Setup in the startup.cs class
- Declare the following variables and fill in the product name and the layer
private string ApplicationName = "ToDos";
private string ApplicationLayer = "Core API";
- Put following code the method ConfigureServices(IServiceCollection services)
// section fwk slogger: Performance logging
services.AddMvc(options =>
{
options.Filters.Add(new TrackPerformanceFilter(ApplicationName, ApplicationLayer));
options.EnableEndpointRouting = false;
}
);
// section fwk slogger: Performance logging
- Put following code the method Configure(IApplicationBuilder app, IWebHostEnvironment env)
services.AddDbContext<ToDoDbContext>(options =>
{
bool enableDbContextLog = bool.Parse(Environment.GetEnvironmentVariable("ENABLE_DBCONTEXT") ?? "false");
if (enableDbContextLog) options.UseLoggerFactory(Logger.GetILoggerFactory());
if (enableDbContextLog) options.EnableSensitiveDataLogging();
options.UseSqlServer(Environment.GetEnvironmentVariable("APPLICATION_DATABASE_CONNECTION"));
});
// section fwk slogger: Global exception handeling
app.UseExceptionHandler(eApp =>
{
eApp.Run(async context =>
{
context.Response.StatusCode = 500;
context.Response.ContentType = "application/json";
var errorCtx = context.Features.Get<IExceptionHandlerFeature>();
if (errorCtx != null)
{
var ex = errorCtx.Error;
WebLogger.LogWebError(ApplicationName, ApplicationLayer, ex, context);
var errorId = Activity.Current?.Id ?? context.TraceIdentifier;
String messageValue = "";
if (env.IsDevelopment())
{
messageValue = errorCtx.Error.Message + " " + errorCtx.Error.StackTrace;
}
else
{
messageValue = "An Server Error Occured, " +
"please contact your system administrator " +
"with following the error code";
}
var jsonResponse = JsonConvert.SerializeObject(new CustomErrorResponse
{
ErrorId = errorId,
Message = messageValue
});
await context.Response.WriteAsync(jsonResponse, Encoding.UTF8);
// For MVC Naveigate to MVC Error Page
// app.UseExceptionHandler("/Home/Error");
}
});
});
// section fwk slogger: Global exception handeling
db context logging, this visualizes the command that are executed against the database
Put following code the the main of program.cs and add the .UseSeriLog() to the host, You also need to add package Serilog.AspNetCore to make this to work
Log.Logger = SLogger.Logger.GetLoggerHostConfig("_<productname>");
Host....
.UseSerilog();
## Enviroment variables to configure
--LOG Destinations--
- ENABLE_LOG_FILE (default : true)
- ENABLE_LOG_MSSQL (default : false)
- ENABLE_LOG_ELASTIC (default : false)
**File Logger**
- LOGFILE_PERF (default : performance.log)
- LOGFILE_USAGE (default : usage.log)
- LOGFILE_ERROR (default : error.log)
- LOGFILE_DIAG (default : diagnostics.log)
- LOGFILE_DBCONTEXT (default : dbcontext.log)
- LOGFILE_HOST (default : host.log)
**MSSQL Logger**
- "LOGDB_CONNECTION": "Server=localhost;Initial Catalog=logging;User Id=sa;Password=[password]",
**ElasticSearch Logger**
- ELASTIC_URL
**Logging Types**
- ENABLE_HOST (default : true)
- ENABLE_DIAG (default : false)
- ENABLE_USAGE (default : false)
- ENABLE_PERF (default : false)
- ENABLE_DBCONTEXT (default : false)
Environement Examples
// Destinations
"ENABLE_LOG_FILE": "true",
"ENABLE_LOG_MSSQL": "true",
"ENABLE_LOG_ELASTIC": "true",
// Types
"ENABLE_DIAG": "true",
"ENABLE_USAGE": "true",
"ENABLE_PERF": "true",
"ENABLE_DBCONTEXT": "true",
// logfilenames
"LOGFILE_DIAG": "performance.log",
"LOGFILE_USAGE": "usage.log",
"LOGFILE_ERROR": "error.log",
"LOGFILE_PERF": "diagnostics.log",
// DB Connectionstring
"LOGDB_CONNECTION": "Server=localhost;Initial Catalog=logging;User Id=sa;Password=[password]",
// Elastic search url
"ELASTICSEARCH_URL" : "http://localhost:9200",
Hosting on IIS
In iis you can configure enviroment variables using the iis manager or add the variables direct in the web.config
<environmentVariables>
<environmentVariable name="ENABLE_DIAG" value="true" />
<environmentVariable name="ENABLE_USAGE" value="true" />
<environmentVariable name="ENABLE_PERF" value="true" />
<environmentVariable name="ENABLE_DBCONTEXT" value="true" />
<environmentVariable name="ENABLE_LOG_FILE" value="true" />
<environmentVariable name="ENABLE_LOG_MSSQL" value="true" />
<environmentVariable name="LOGDB_CONNECTION" value="Server=localhost;Initial Catalog=identityserver4-logging;User Id=sa;Password=[03879e0aa70850ed0357ffX74d11fe90]" />
<environmentVariable name="ENABLE_LOG_ELASTIC" value="false" />
<environmentVariable name="ELASTICSEARCH_URL" value="http://localhost:9200" />
</environmentVariables>
How to use
Usage Tracking
With this data annotation you can tag the webmethods for the usage logging
[TrackUsage("ToDos", "API Todo", "Retrieve Todo List")]
[TrackUsage(ApplicationName, ApplicationLayer, "Retrieve Todo List")]
Diagnostics Logging
Performance Tracking
automaticly used what setup is done, his can be turned on and off in the enviroment variables
Error Logging
automaticly used what setup is done, This can be turned on and off in the enviroment variables
Product | Versions 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. 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. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Microsoft.AspNetCore.Diagnostics (>= 2.2.0)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.AspNetCore.Routing (>= 2.2.2)
- Serilog (>= 2.9.0)
- Serilog.Extensions.Logging (>= 3.0.1)
- Serilog.Sinks.Console (>= 3.1.1)
- Serilog.Sinks.ElasticSearch (>= 8.0.1)
- Serilog.Sinks.File (>= 4.1.0)
- Serilog.Sinks.MSSqlServer (>= 5.1.3)
- System.Data.SqlClient (>= 4.8.0)
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 |
---|---|---|
1.0.2 | 679 | 3/10/2020 |