Jinget.Logger 6.2.19-preview026

Prefix Reserved
This is a prerelease version of Jinget.Logger.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Jinget.Logger --version 6.2.19-preview026
                    
NuGet\Install-Package Jinget.Logger -Version 6.2.19-preview026
                    
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="Jinget.Logger" Version="6.2.19-preview026" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Jinget.Logger" Version="6.2.19-preview026" />
                    
Directory.Packages.props
<PackageReference Include="Jinget.Logger" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Jinget.Logger --version 6.2.19-preview026
                    
#r "nuget: Jinget.Logger, 6.2.19-preview026"
                    
#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.
#addin nuget:?package=Jinget.Logger&version=6.2.19-preview026&prerelease
                    
Install Jinget.Logger as a Cake Addin
#tool nuget:?package=Jinget.Logger&version=6.2.19-preview026&prerelease
                    
Install Jinget.Logger as a Cake Tool

Jinget Logger

Using this library, you can easily save your application logs in Elasticsearch database or files.

How to Use:

Download the package from NuGet using Package Manager: Install-Package Jinget.Logger You can also use other methods supported by NuGet. Check Here for more information.

Configuration

Config logging destination:

Elasticsearch:

LogToElasticSearch: By calling this method, you are going to save your logs in Elasticsearch

builder.Host.LogToElasticSearch<OperationLog, ErrorLog, CustomLog>(blacklist);

blacklist: Log messages contain the blacklist array items will not logged.

allowedLoglevels: Defines an array contains allowed log levels. If log's severity exists in this array, then it will be saved in elasticsearch otherwise it will be ignored. If this parameter not set, then all log levels will be allowed.

After setting the logging destination, you need to configure Elasticsearch:

builder.Services.ConfigureElasticSearchLogger<OperationLog, ErrorLog, CustomLog>(
    new ElasticSearchSettingModel
    {
        UserName = <authentication username>,
        Password = <authentication password>,
        Url = <ElasticSearch Url>,
        UseSsl = <true|false>,
        RegisterDefaultLogModels = <true|false>,
        DiscoveryTypes = new List<Type> { typeof(OperationLog) }
    });

Url: Elasticsearch service url. This address should not contains the PROTOCOL itself. Use 'abc.com:9200' instead of 'http://abc.com:9200'

UserName: Username, if basic authentication enabled on Elasticsearch search service

Password: Password, if basic authentication enabled on Elasticsearch search service

UseSsl: Set whether to use SSL while connecting to Elasticsearch or not

RegisterDefaultLogModels: You can configure logging using your own models instead of OperationLog, ErrorLog or CustomLog. In order to do so, you can simple create derived types and use them instead of these types. When you are working with your own custom types, if you want to create index for default log models, you can set the RegisterDefaultLogModels property to true, otherwise you can set it as false.

DiscoveryTypes: Foreach type specified in this list, an index in Elasticsearch will be created

CreateIndexPerPartition: Create index per partition using HttpContext.Items["jinget.log.partitionkey"] value. If this mode is selected, then RegisterDefaultLogModels and also DiscoveryTypes will not be used. If this mode is selected, then index creation will be deferred until the first document insertion. foreach partition key, a separated index will be created. all of the indexes will share the same data model. for request/response logs, Entities.Log.OperationLog will be used. for errors, Entities.Log.ErrorLog will be used. for custom logs, Entities.Log.CustomLog will be used.

If you want to use partition key, instead of predefined/custom models, then you do not need to pass the generic types. Just like below:

builder.Host.LogToElasticSearch(blacklist);
...

var elasticSearchSetting = new ElasticSearchSettingModel
{
    CreateIndexPerPartition = true,
    UserName = <authentication username>,
    Password = <authentication password>,
    Url = <ElasticSearch Url>,
    UseSsl = <true|false>
};
builder.Services.ConfigureElasticSearchLogger(elasticSearchSetting);

And finally you need to add the Jinget.Logger middleware to your pipeline:

app.UseJingetLogging();

If you are using partition key, then you need to set your partition key before calling app.UseJingetLogging(). Like below:

app.UseWhen(p => elasticSearchSetting.CreateIndexPerPartition, appBuilder =>
{
    appBuilder.Use(async (context, next) =>
    {
        //define the partitioning logic
        bool partitionKeyExists = context.Request.Headers.TryGetValue("jinget.client_id", out StringValues partitionKey);
        if (partitionKeyExists)
            context.Items.Add("jinget.log.partitionkey", $"test.{partitionKey}");

        await next.Invoke();
    });
});

For example in the above code, logs will be partitioned based on the jinget.client_id header's value. If this header does not exists in the request, the default index name will be used which are created using the following code:

$"{AppDomain.CurrentDomain.FriendlyName}.{typeof(TModelType).Name}".ToLower();

When using partition key, index names will be constructed as below:

//for operation log
$"op.{partitionKey}".ToLower();

//for error log
$"err.{partitionKey}".ToLower();

//for custom log
$"cus.{partitionKey}".ToLower();

Here is the complete configuration for a .NET Web API application:

Without Partitioning:

using Jinget.Core.Filters;
using Jinget.Logger.Configuration;
using Jinget.Logger.Entities.Log;

var builder = WebApplication.CreateBuilder(args);

var config = new ConfigurationBuilder().AddJsonFile("appsettings.json", false, true).Build();

var blacklist = config.GetSection("logging:BlackList").Get<string[]>();
builder.Host.LogToElasticSearch<OperationLog, ErrorLog, CustomLog>(blacklist);

var elasticSearchSetting = new ElasticSearchSettingModel
{
    UserName = "myuser",
    Password = "mypass",
    Url = "192.168.1.1:9200",
    UseSsl = false,
    RegisterDefaultLogModels = false,
    DiscoveryTypes = new List<Type> { typeof(OperationLog) }
};
builder.Services.ConfigureElasticSearchLogger<OperationLog, ErrorLog, CustomLog>(elasticSearchSetting);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();

var app = builder.Build();

app.UseJingetLogging();
app.MapControllers();
app.Run();

With Partitioning:

using Jinget.Core.Filters;
using Jinget.Logger.Configuration;
using Jinget.Logger.Configuration.Middlewares.ElasticSearch;
using Jinget.Logger.Entities.Log;
using Jinget.Logger.Handlers.CommandHandlers;
using Microsoft.Extensions.Primitives;

var builder = WebApplication.CreateBuilder(args);

var config = new ConfigurationBuilder().AddJsonFile("appsettings.json", false, true).Build();

var blacklist = config.GetSection("logging:BlackList").Get<string[]>();

builder.Host.LogToElasticSearch(blacklist);
var elasticSearchSetting = new ElasticSearchSettingModel
{
    CreateIndexPerPartition = true,
    UserName = "myuser",
    Password = "mypass",
    Url = "192.168.1.1:9200",
    UseSsl = false
};
builder.Services.ConfigureElasticSearchLogger(elasticSearchSetting);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();

var app = builder.Build();

app.UseWhen(p => elasticSearchSetting.CreateIndexPerPartition, appBuilder =>
{
    appBuilder.Use(async (context, next) =>
    {
        bool partitionKeyExists = context.Request.Headers.TryGetValue("jinget.client_id", out StringValues partitionKey);
        if (partitionKeyExists)
            context.Items.Add("jinget.log.partitionkey", $"test.{partitionKey}");

        await next.Invoke();
    });
});

app.UseJingetLogging();
app.MapControllers();
app.Run();

File:

LogToFile: By calling this method, you are going to save your logs in files

builder.Host.LogToFile(blacklist, fileNamePrefix: "Log-", logDirectory: "D:\\logs", 10, 15);

blacklist: Log messages contain the blacklist array items will not logged.

allowedLoglevels: Defines an array contains allowed log levels. If log's severity exists in this array, then it will be saved in file otherwise it will be ignored. If this parameter not set, then all log levels will be allowed.

fileNamePrefix: Gets or sets the filename prefix to use for log files. Defaults is logs-

logDirectory: The directory in which log files will be written, relative to the app process. Default is Logs directory.

retainedFileCountLimit: Gets or sets a strictly positive value representing the maximum retained file count or null for no limit. Defaults is 2 files.

fileSizeLimit: Gets or sets a strictly positive value representing the maximum log size in MB or null for no limit. Once the log is full, no more messages will be appended. Defaults is 10MB.

After setting the logging destination, you need to configure Elasticsearch:

builder.Services.ConfigureFileLogger();

Here is the complete configuration for a .NET Web API application:

using Jinget.Core.Filters;
using Jinget.Logger.Configuration;

var builder = WebApplication.CreateBuilder(args);

var config = new ConfigurationBuilder().AddJsonFile("appsettings.json", false, true).Build();

var blacklist = config.GetSection("logging:BlackList").Get<string[]>();
builder.Host.LogToFile(blacklist, "Log-", "D:\\logs", 10, 15);
builder.Services.ConfigureFileLogger();

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();

var app = builder.Build();

app.UseJingetLogging();

app.UseAuthorization();
app.MapControllers();
app.Run();

How to install

In order to install Jinget Logger please refer to nuget.org

Contact Me

👨‍💻 Twitter: https://twitter.com/_jinget

📧 Email: farahmandian2011@gmail.com

📣 Instagram: https://www.instagram.com/vahidfarahmandian

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.  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. 
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
8.0.24 103 5/31/2025
8.0.23 137 5/28/2025
8.0.23-preview001 130 5/28/2025
8.0.22 148 5/25/2025
8.0.21 143 5/11/2025
8.0.20 130 5/11/2025
8.0.19 132 5/11/2025
8.0.18 168 4/21/2025
8.0.17 162 4/21/2025
8.0.16 97 4/19/2025
8.0.15 203 4/13/2025
8.0.14 167 4/7/2025
8.0.13 152 4/7/2025
8.0.12 100 4/5/2025
8.0.11 205 3/9/2025
8.0.10 176 3/9/2025
8.0.9 185 3/9/2025
8.0.8 152 2/27/2025
8.0.7 114 2/27/2025
8.0.6 110 2/27/2025
8.0.5 114 2/26/2025
8.0.4 115 2/25/2025
8.0.3 108 2/24/2025
8.0.2 103 2/24/2025
8.0.1 138 2/20/2025
8.0.0 167 1/22/2025
8.0.0-preview013 202 12/19/2024
8.0.0-preview012 96 12/19/2024
8.0.0-preview011 95 12/17/2024
8.0.0-preview010 109 12/16/2024
8.0.0-preview009 91 12/15/2024
8.0.0-preview008 105 11/25/2024
8.0.0-preview007 142 11/11/2024
8.0.0-preview006 121 11/2/2024
8.0.0-preview005 93 11/2/2024
8.0.0-preview004 94 11/1/2024
8.0.0-preview003 99 11/1/2024
8.0.0-preview002 86 11/1/2024
8.0.0-preview001 87 11/1/2024
6.2.23-preview003 91 10/31/2024
6.2.23-preview002 91 10/31/2024
6.2.22 135 10/26/2024
6.2.21 113 10/26/2024
6.2.20 115 10/26/2024
6.2.19-preview037 93 10/14/2024
6.2.19-preview036 90 9/30/2024
6.2.19-preview035 112 9/10/2024
6.2.19-preview034 105 9/9/2024
6.2.19-preview033 110 9/1/2024
6.2.19-preview032 113 9/1/2024
6.2.19-preview031 111 8/31/2024
6.2.19-preview029 109 8/26/2024
6.2.19-preview028 124 8/26/2024
6.2.19-preview027 135 8/26/2024
6.2.19-preview026 137 8/21/2024
6.2.19-preview025 128 8/21/2024
6.2.19-preview024 124 8/19/2024
6.2.19-preview023 109 8/8/2024
6.2.19-preview022 118 8/8/2024
6.2.19-preview021 94 8/5/2024
6.2.19-preview020 86 8/5/2024
6.2.19-preview019 97 8/5/2024
6.2.19-preview018 71 8/3/2024
6.2.19-preview017 88 7/30/2024
6.2.19-preview016 96 7/29/2024
6.2.19-preview015 97 7/29/2024
6.2.19-preview014 106 7/26/2024
6.2.19-preview013 105 7/20/2024
6.2.19-preview012 100 7/20/2024
6.2.19-preview011 118 6/15/2024
6.2.19-preview010 106 6/14/2024
6.2.19-preview009 104 6/14/2024
6.2.19-preview008 105 6/13/2024
6.2.19-preview007 103 6/13/2024
6.2.19-preview006 104 6/13/2024
6.2.19-preview005 100 6/13/2024
6.2.19-preview004 104 6/13/2024
6.2.19-preview003 114 6/11/2024
6.2.19-preview002 108 6/8/2024
6.2.19-preview001 110 6/8/2024
6.2.18 159 6/6/2024
6.2.18-preview020 114 6/6/2024
6.2.18-preview019 118 6/6/2024
6.2.18-preview018 119 6/6/2024
6.2.18-preview017 117 6/2/2024
6.2.18-preview016 117 6/1/2024
6.2.18-preview015 123 5/28/2024
6.2.18-preview014 121 5/28/2024
6.2.18-preview013 118 5/28/2024
6.2.18-preview012 116 5/28/2024
6.2.18-preview011 121 5/26/2024
6.2.18-preview010 116 5/26/2024
6.2.18-preview009 119 5/26/2024
6.2.18-preview008 119 5/26/2024
6.2.18-preview007 136 5/22/2024
6.2.18-preview006 114 5/22/2024
6.2.18-preview005 131 5/19/2024
6.2.18-preview004 119 5/19/2024
6.2.18-preview003 119 5/19/2024
6.2.18-preview002 119 5/19/2024
6.2.17 151 5/19/2024
6.2.16 149 5/18/2024
6.2.15 148 5/18/2024
6.2.14 149 5/18/2024
6.2.13 147 5/17/2024
6.2.12 149 5/17/2024
6.2.11 150 5/17/2024
6.2.10 145 5/17/2024
6.2.9 125 5/12/2024
6.2.8 140 5/9/2024
6.2.7 132 5/9/2024
6.2.6 144 5/7/2024
6.2.5 147 4/24/2024
6.2.4 222 2/1/2024
6.2.1 154 1/23/2024
6.2.0 141 1/23/2024
6.2.0-preview013 115 1/19/2024
6.2.0-preview012 111 1/19/2024
6.2.0-preview011 110 1/18/2024
6.2.0-preview010 118 1/14/2024
6.2.0-preview009 120 1/11/2024
6.2.0-preview008 130 1/1/2024
6.2.0-preview007 105 1/1/2024
6.2.0-preview006 119 1/1/2024
6.2.0-preview005 129 1/1/2024
6.2.0-preview001 141 12/30/2023
6.1.0 241 12/2/2023
6.1.0-preview003 149 12/2/2023
6.1.0-preview002 125 12/2/2023
6.1.0-preview001 144 12/2/2023
6.0.2 171 11/27/2023
6.0.1 174 11/22/2023
6.0.0 169 11/22/2023
3.5.0 198 10/28/2023
3.4.0 172 10/1/2023
3.3.1 184 9/30/2023
3.3.0 179 9/28/2023
3.2.5 177 9/28/2023
3.2.4 159 9/28/2023
3.2.3 169 9/28/2023
3.2.2 164 9/28/2023
3.2.1 159 9/28/2023
3.2.0 189 9/28/2023
3.1.0 172 9/27/2023
3.0.1 178 9/27/2023
3.0.0 181 9/14/2023
3.0.0-preview001 158 9/14/2023