JbsnLogger 1.1.3

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

// Install JbsnLogger as a Cake Tool
#tool nuget:?package=JbsnLogger&version=1.1.3                

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 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. 
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
1.1.3 221 12/16/2023 1.1.3 is deprecated because it is no longer maintained.
1.1.2 100 12/16/2023
1.1.1 107 12/10/2023
1.1.0 95 12/10/2023
1.0.16 96 12/10/2023
1.0.15 101 12/10/2023
1.0.14 105 12/10/2023
1.0.13 109 12/10/2023
1.0.12 104 12/10/2023
1.0.11 94 12/10/2023
1.0.10 103 12/10/2023
1.0.9 107 12/10/2023
1.0.8 103 12/10/2023
1.0.7 111 12/10/2023
1.0.6 111 12/10/2023
1.0.5 104 12/10/2023
1.0.4 116 12/10/2023
1.0.3 97 12/10/2023
1.0.2 110 12/10/2023
1.0.1 85 12/10/2023
1.0.0 123 12/5/2023
0.0.2 123 12/5/2023
0.0.1 99 12/5/2023