JbsnLogger 1.1.2
See the version list below for details.
dotnet add package JbsnLogger --version 1.1.2
NuGet\Install-Package JbsnLogger -Version 1.1.2
<PackageReference Include="JbsnLogger" Version="1.1.2" />
paket add JbsnLogger --version 1.1.2
#r "nuget: JbsnLogger, 1.1.2"
// Install JbsnLogger as a Cake Addin #addin nuget:?package=JbsnLogger&version=1.1.2 // Install JbsnLogger as a Cake Tool #tool nuget:?package=JbsnLogger&version=1.1.2
What is Logger?
Logger is a solution for you to save error logs or information from your application.
How do I get started?
First, you need to create an application key to save the logs, visit https://jbsn-logger.vercel.app
Then you can use the initialize method to enter your key:
Logger.Initialize("186a788f-9ee9-46e1-a6ca-9fa960df5a17");
Then you can use the LogError or LogInfo methods to log your application:
Logger.Log(LogLevel.Error, "Fetching data...", ex, Guid.NewGuid()); // Generic log
Logger.LogError("Fetching data...", ex, Guid.NewGuid()); // Log error
Logger.LogInfo("Fetching data...", null, Guid.NewGuid()); // Log info
It is possible to report an error message or an exception, in which case it will be logged as a text
For structured logs, enter your message and eventId, or exception and eventId.
The maximum character limit for a log is 8,000 character.
Middleware
It is possible to save request data from your API using the LoggerRouteInfo class
string? BodyRequest;
string? BodyResponse;
string Path;
string? QueryString;
TimeSpan? Duration;
int? StatusCode;
IEnumerable<KeyValuePair<string, StringValues>>? Headers;
Complete middleware example
Inject the logger middleware into your project
app.InjectLoggerMiddleware();
Create the MiddlewareExtension
public static class MiddlewareExtension
{
public static IApplicationBuilder InjectLoggerMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<LoggerMiddleware>();
}
}
Creates LoggerMiddleware
public class LoggerMiddleware
{
private readonly RequestDelegate _next;
public LoggerMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext httpContext)
{
try
{
var bodyRequest = await GetBodyRequest(httpContext);
var memoryStreamBodyResponse = new MemoryStream();
var bodyStream = httpContext.Response.Body;
httpContext.Response.Body = memoryStreamBodyResponse;
var stopwatch = new Stopwatch();
stopwatch.Start();
await _next(httpContext);
stopwatch.Stop();
var path = httpContext.Request.Path.ToString().ToLower();
var bodyResponse = await GetBodyResponse(httpContext, bodyStream);
var statusCode = httpContext.Response.StatusCode;
var queryString = httpContext.Request.QueryString.ToString();
var level = statusCode >= 200 && statusCode <= 299 ? LoggerLevel.Info: LoggerLevel.Error;
var routeInfo = new LoggerRouteInfoDto
{
BodyRequest = bodyRequest,
BodyResponse = bodyResponse,
Duration = stopwatch.Elapsed,
Path = path,
QueryString = queryString,
StatusCode = statusCode,
Headers = httpContext.Request.Headers
};
_ = Logger.Log(level, routeInfo: routeInfo);
}
catch (Exception ex)
{
_ = Logger.LogError(exception: ex);
}
}
protected async Task<string?> GetBodyRequest(HttpContext httpContext)
{
string? response = null;
if (httpContext.Request.ContentLength > 0)
{
var memoryStream = new MemoryStream();
await httpContext.Request.Body.CopyToAsync(memoryStream);
memoryStream.Position = 0;
response = await new StreamReader(memoryStream).ReadToEndAsync();
memoryStream.Position = 0;
httpContext.Request.Body = memoryStream;
}
return response;
}
protected async Task<string?> GetBodyResponse(HttpContext httpContext, Stream bodyStream)
{
string? response = null;
if (httpContext.Response.Body != null && httpContext.Response.Body.Length > 0)
{
httpContext.Response.Body.Position = 0;
response = await new StreamReader(httpContext.Response.Body).ReadToEndAsync();
httpContext.Response.Body.Position = 0;
await httpContext.Response.Body.CopyToAsync(bodyStream);
httpContext.Response.Body = bodyStream;
}
return response;
}
}
Where can I get it?
First, install NuGet. Then, install JbsnLogger from the package manager console:
PM> Install-Package JbsnLogger
Or from the .NET CLI as:
dotnet add package JbsnLogger
License, etc.
This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community. For more information see the .NET Foundation Code of Conduct.
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.Primitives (>= 8.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.
Version | Downloads | Last updated | |
---|---|---|---|
1.1.3 | 245 | 12/16/2023 | |
1.1.2 | 117 | 12/16/2023 | |
1.1.1 | 124 | 12/10/2023 | |
1.1.0 | 112 | 12/10/2023 | |
1.0.16 | 114 | 12/10/2023 | |
1.0.15 | 119 | 12/10/2023 | |
1.0.14 | 121 | 12/10/2023 | |
1.0.13 | 129 | 12/10/2023 | |
1.0.12 | 119 | 12/10/2023 | |
1.0.11 | 111 | 12/10/2023 | |
1.0.10 | 118 | 12/10/2023 | |
1.0.9 | 124 | 12/10/2023 | |
1.0.8 | 115 | 12/10/2023 | |
1.0.7 | 126 | 12/10/2023 | |
1.0.6 | 125 | 12/10/2023 | |
1.0.5 | 118 | 12/10/2023 | |
1.0.4 | 131 | 12/10/2023 | |
1.0.3 | 115 | 12/10/2023 | |
1.0.2 | 129 | 12/10/2023 | |
1.0.1 | 102 | 12/10/2023 | |
1.0.0 | 142 | 12/5/2023 | |
0.0.2 | 139 | 12/5/2023 | |
0.0.1 | 117 | 12/5/2023 |