Pixelo.Utils.CustomLogger 1.0.9

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

// Install Pixelo.Utils.CustomLogger as a Cake Tool
#tool nuget:?package=Pixelo.Utils.CustomLogger&version=1.0.9                

INTRODUCTION

With this library you can add registry for any activities with 4 different channels :

  • Write on file
  • Write on DB
  • Send Email
  • Send telegram on Bot

HOW TO

To set parameters you need to set two things:

  1. You must edit appsettings.json on your WebApi ( this object must insert inside Logging, like this ) :
"CustomLogger": {
      "Options": {
        "schemaNameTable": "",
        "ApplicationName": "Design Pattern",
        "ConnectionString": "Data Source=<YOURIP>;Initial Catalog=<YOURDB>;Persist Security Info=True;User ID=sa;Password=<YOURPASSWORD>;",
        "fileOptions": {
          "FilePath": "log_{date}.log",
          "FolderPath": "C:\\logs\\"
        },
        "MailSetting": {
          "SMTPassword": "SMTPassword",
          "SMTPEnabledSSL": true,
          "SMTPHOST": "SMTPHOST",
          "SMTPMitt": "SMTPMitt",
          "SMTPMittDisplayName": "Design Pattern Alerts",
          "SMTPPORT": 587,
          "SMTPSendEmailTo": "a.chiodo@pixelo.it",
          "xsltPath": "C:\\Source\\Repos\\CustomLogger\\WebApiTest\\xslt\\mail.xslt",
          "SecondsCachingBetweenMessagesOnSameApplication": 6
        },
        "telegramOptions": {
          "TotRetry": [
            "00:00:10.0000000",
            "00:00:20.0000000"
          ],
          "httpStatusRetry": 429,
          "SecondsCachingBetweenMessagesOnSameApplication": 10,
          "chatId": "55555555555",
          "Api_Key": "387563510:AAENrZ3EOWksWA_5a001xxxxxxxxxxxxxxx"
        },
        "LogLevels": {
          "Information": [
            "SendToFile"
          ],
          "Warning": [
            "SendToFile",
            "SendToDB"
          ],
          "Error": [
            "SendToFile",
            "SendToDB",
            "SendEmail"
          ],
          "Critical": [
            "SendToFile",
            "SendToDB",
            "SendEmail",
            "SendTelegram"
          ]
        }
      }
    }

Add on program.cs this section :

//HTTPFACTORY
builder.Services.AddTransient<LoggerHandler>();
builder.Services.AddHttpClient<IhttpsClientHelper, httpsClientHelper>("httpsWithCertificate").ConfigureHttpClient(
    client => { })
    .ConfigurePrimaryHttpMessageHandler(() =>
        new httpBindingOptions().getCertificateForHttpHandler(
            builder.Configuration.GetSection("httpHelperOptions:certificatePath").Value,
            builder.Configuration.GetSection("httpHelperOptions:certificatepassword").Value
            )
).AddHttpMessageHandler<LoggerHandler>();
//HTTPFACTORY
//CUSTOMLOGGER
builder.Logging.AddCustomLogger(options => {
    builder
    .Configuration
    .GetSection("Logging")
    .GetSection("CustomLogger")
    .GetSection("Options")
    .Bind(options);
});
//CUSTOMLOGGER
//MIDDLEWARE
app.UseMiddleware<CustomLoggingMiddleWare>();
//MIDDLEWARE

DB objects:

Include on your DB these tables:

CREATE TABLE [dbo].[tTracert](
	[Id] [bigint] IDENTITY(1,1) NOT NULL,
	[MethodName] [varchar](max) NOT NULL,
	[LevelLog] [tinyint] NOT NULL,
	[Message] [varchar](max) NOT NULL,
	[TimeLog] [datetime] NOT NULL,
	[idTransazione] [varchar](max) NULL,
 CONSTRAINT [PK_tTracert_New] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)
GO
CREATE TABLE [dbo].[tTraceMessage](
	[Id] [bigint] IDENTITY(1,1) NOT NULL,
	[Request] [varchar](max) NULL,
	[Response] [varchar](max) NOT NULL,
	[TimeRequest] [datetime] NOT NULL,
	[TimeResponse] [datetime] NOT NULL,
	[Action] [varchar](max) NOT NULL,
	[IsHttpError] [bit] NULL,
	[reference] [varchar](250) NULL,
 CONSTRAINT [PK_tTraceMessage_New] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)

First table is for insert logs, second table is to trace any http request external on your project

An Example on your controller on web api can be :

    [ApiController]
    [Route("[controller]")]
    public class SampleController : ControllerBase {
        IhttpsClientHelper http;
        protected ILogger<Pixelo.Utils.CustomLogger> logger;
        public SampleController(ILogger<Pixelo.Utils.CustomLogger> _logger, IhttpsClientHelper _httpclient) {
            logger = _logger;
            http = _httpclient;
        }
        [HttpGet(Name = "GetLogs")]
        public IActionResult getLogs(string message, LogLevel logLevel) {
            logger.Log(logLevel, message);
            return Ok();
        }
        [HttpPost]
        public async Task<IActionResult> CallExternalHttp(string url) {
            HttpResponseMessage httpResponseMessage = await http.sendAsync(url, "application/json", "GET");
            if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
                return Ok(httpResponseMessage.Content.ReadAsStringAsync());
            else
                return Ok(httpResponseMessage.StatusCode.ToString());
        }
    }

CONSIDERATIONS

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.0.9 308 1/3/2023
1.0.8 265 12/30/2022
1.0.7 268 12/29/2022
1.0.6 288 12/29/2022
1.0.5 291 12/28/2022
1.0.4 275 12/27/2022
1.0.3 255 12/27/2022
1.0.2 271 11/16/2022
1.0.0 288 11/16/2022

Added httpFactory