CSharpEssentials.LoggerHelper
2.0.7
See the version list below for details.
dotnet add package CSharpEssentials.LoggerHelper --version 2.0.7
NuGet\Install-Package CSharpEssentials.LoggerHelper -Version 2.0.7
<PackageReference Include="CSharpEssentials.LoggerHelper" Version="2.0.7" />
<PackageVersion Include="CSharpEssentials.LoggerHelper" Version="2.0.7" />
<PackageReference Include="CSharpEssentials.LoggerHelper" />
paket add CSharpEssentials.LoggerHelper --version 2.0.7
#r "nuget: CSharpEssentials.LoggerHelper, 2.0.7"
#:package CSharpEssentials.LoggerHelper@2.0.7
#addin nuget:?package=CSharpEssentials.LoggerHelper&version=2.0.7
#tool nuget:?package=CSharpEssentials.LoggerHelper&version=2.0.7
π¦ CSharpEssentials.LoggerHelper
π Version History
- 1.1.2 β Added Middleware
- 1.1.4 β Removed
TraceAsynconfinallyblock ofRequestResponseLoggingMiddleware - 1.1.6 β Fixed issues detected by CodeQL
- 1.2.1 β Optimized with test Web API
- 1.2.2 β Optimized
Propertieshandling and Email sink - 1.3.1 β Added compatibility with .NET 6.0
- 2.0.0 β Fixed Email configuration and sink behavior
- 2.0.2 β Optimized HTML template for middleware
- 2.0.4 β Rollback: removed .NET 7.0 support
- 2.0.5 β Fixed
IRequestinterface - 2.0.6 β Added external email template support
<a id='table-of-contents'></a>
π Table of Contents
- πIntroduction
- πInstallation
- πPostgreSQL Sink
- π£ Telegram Sink
- π¨ HTML Email Sink
- πΎ MS SQL Sink
- π ElasticSearch Sink
- π§ͺ Demo API
π Introduction<a id='introduction'></a> π
LoggerHelper is a flexible and modular structured logging library for .NET (6.0/8.0) applications based on Serilog. It enables structured, multi-sink logging through a plug-and-play approach.
π Key Benefits:
- β
Structured logs:
Action,IdTransaction,ApplicationName,MachineName - β Multi-sink: Console, File, Email (HTML), PostgreSQL, ElasticSearch, Telegram
- β
Placeholder validation: avoids runtime
{}mismatch errors - β
One config file:
appsettings.LoggerHelper.json - β
Modular integration via
LoggerBuilder
β οΈ Important for developers: In development mode, LoggerHelper automatically uses
appsettings.LoggerHelper.debug.json. This allows safe testing without affecting production settings.
#if DEBUG
.AddJsonFile("appsettings.LoggerHelper.debug.json")
#else
.AddJsonFile("appsettings.LoggerHelper.json")
#endif
π Installation <a id='installation'></a> π
dotnet add package CSharpEssentials.LoggerHelper
βοΈ Configuration
The full configuration JSON can be found in the original README. Important:
- Define the
SerilogConditionfor each sink with the desiredLevel - If
Levelis empty, the sink is ignored
βοΈ General Setup
To activate LoggerHelper and enable request/response logging, configure your application in Program.cs as follows:
#if NET6_0
builder.AddLoggerConfiguration();
#else
builder.Services.AddLoggerConfiguration(builder);
#endif
Enable HTTP middleware logging:
app.UseMiddleware<RequestResponseLoggingMiddleware>();
Example appsettings.LoggerHelper.json configuration (β οΈ or appsettings.LoggerHelper.debug.json during development):
{
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Debug",
"System": "Debug"
}
},
"SerilogConfiguration": {
"ApplicationName": "TestApp",
"SerilogCondition": [
{"Sink": "ElasticSearch","Level": []},
{"Sink": "MSSqlServer","Level": []},
{"Sink": "Email","Level": []},
{"Sink": "PostgreSQL","Level": ["Information","Warning","Error","Fatal"]},
{"Sink": "Telegram","Level": ["Fatal"]},
{"Sink": "Console","Level": [ "Information" ]},
{"Sink": "File","Level": ["Information","Warning","Error","Fatal"]}
],
"SerilogOption": {
"File": {
"Path": "D:\Logs\ServerDemo",
"RollingInterval": "Day",
"RetainedFileCountLimit": 7,
"Shared": true
},
"TelegramOption": {
"chatId": "xxxxx",
"Api_Key": "sssss:ttttttttt"
},
"PostgreSQL": {
"connectionString": "<YOUR CONNECTIONSTRING>",
"tableName": "public",
"schemaName": "dbo",
"needAutoCreateTable": true,
"addAutoIncrementColumn": true,
"ColumnsPostGreSQL": [
{"Name": "Message","Writer": "Rendered","Type": "text"},
{"Name": "MessageTemplate","Writer": "Template","Type": "text"},
{"Name": "Level","Writer": "Level","Type": "varchar"},
{"Name": "TimeStamp","Writer": "timestamp","Type": "timestamp"},
{"Name": "Exception","Writer": "Exception","Type": "text"},
{"Name": "Properties","Writer": "Properties","Type": "jsonb"},
{"Name": "LogEvent","Writer": "Serialized","Type": "jsonb"},
{"Name": "IdTransaction","Writer": "Single","Property": "IdTransaction","Type": "varchar"},
{"Name": "MachineName","Writer": "Single","Property": "MachineName","Type": "varchar"},
{"Name": "Action","Writer": "Single","Property": "Action","Type": "varchar"},
{"Name": "ApplicationName","Writer": "Single","Property": "ApplicationName","Type": "varchar"}
]
},
"ElasticSearch": {
"nodeUris": "http://10.0.1.100:9200",
"indexFormat": "<YOUR INDEX FORMAT>"
},
"Email": {
"From": "<Email Alert>",
"Port": 587,
"Host": "<Host EMail>",
"To": [ "recipient#1", "recipient#2" ],
"username": "<UserName SMTP>",
"password": "<Password SMTP>"
},
"MSSqlServer": {
"connectionString": "<YOUR CONNECTIONSTRING>",
"sinkOptionsSection": {
"tableName": "logs",
"schemaName": "dbo",
"autoCreateSqlTable": true,
"batchPostingLimit": 100,
"period": "0.00:00:10"
},
"columnOptionsSection": {
"addStandardColumns": ["LogEvent"],
"removeStandardColumns": ["Properties"]
}
},
"GeneralConfig": {
"EnableSelfLogging": false
}
}
}
}
}
π PostgreSQL Sink<a id='postgresql-sink'></a> π
LoggerHelper supports logging to PostgreSQL with optional custom schema definition.
If
ColumnsPostGreSQLis not set, the following default columns will be created and used:message,message_template,level,raise_date,exception,properties,props_test,machine_name
If
ColumnsPostGreSQLis defined, LoggerHelper will use the exact fields provided.Setting
addAutoIncrementColumn: truewill add anid SERIAL PRIMARY KEYautomatically.
Example configuration:
"PostgreSQL": {
"connectionString": "...",
"tableName": "Logs",
"schemaName": "public",
"addAutoIncrementColumn": true,
"ColumnsPostGreSQL": [
{ "Name": "Message", "Writer": "Rendered", "Type": "text" },
{ "Name": "Level", "Writer": "Level", "Type": "varchar" }
]
}
π§ͺ PostgreSQL Table Structure
If custom ColumnsPostGreSQL is defined, logs will include all specified fields.
π§© Tip: PostgreSQL sink is ideal for deep analytics and long-term log storage. β οΈ Note: When using
ColumnsPostGreSQL, always enableSelfLogduring development to detect unsupported or misconfigured column definitions. Invalid types or property names will be silently ignored unless explicitly logged via Serilogβs internal diagnostics.
π Telegram Sink<a id='telegram-sink'></a> π
LoggerHelper supports Telegram notifications to alert on critical events.
β οΈ Recommended Levels: Use only
ErrororFatalto avoid exceeding Telegram rate limits.
π Example Configuration
"TelegramOption": {
"chatId": "<YOUR_CHAT_ID>",
"Api_Key": "<YOUR_BOT_API_KEY>"
}
To configure a Telegram Bot:
- Open Telegram and search for @BotFather
- Create a new bot and copy the API token
- Use https://api.telegram.org/bot<YourBotToken>/getUpdates to get your chat ID after sending a message to the bot
πΈ Example of a formatted Telegram message:
π‘ Usage Examples
_logger.TraceSync(
new Request {
IdTransaction = Guid.NewGuid().ToString(),
Action = "SampleAction",
ApplicationName = "MyApp"
},
LogEventLevel.Information,
null,
"Sample log message: {Parameter}",
123
);
Or async:
await _logger.TraceAsync(
request,
LogEventLevel.Error,
ex,
"Something failed: {ErrorMessage}",
ex.Message
);
π¨ HTML Email Sink<a id='html-email-sink'></a> π
β οΈ Version 2.0.0 - Breaking Change
Starting from version 2.0.0, the
If you are upgrading from
1.x.x, you MUST update yourappsettings.LoggerHelper.json.
Old (before 2.0.0):
"Email": {
"From": "...",
"Host": "...",
"Port": 587,
"To": ["..."],
"CredentialHost": "...",
"CredentialPassword": "..."
}
New (since 2.0.0):
"Email": {
"From": "...",
"Host": "...",
"Port": 587,
"To": "...",
"username": "...",
"password": "...",
"EnableSsl": true
}
π¨ Why Email Handling Changed
Starting from version 2.0.0, LoggerHelper no longer uses the standard Serilog.Sinks.Email for sending emails.
Reason:
The official Serilog Email Sink does not support custom body formatting (HTML templates, structured logs, color coding, etc).
It only supports plain text messages generated via RenderMessage(), without the ability to control the message content.
π See discussion: GitHub Issue - serilog/serilog-sinks-email
What changed:
- LoggerHelper now uses a custom internal SMTP sink:
LoggerHelperEmailSink. - This allows sending fully customized HTML-formatted emails.
- Supports dynamic coloring based on log level (Information, Warning, Error).
- Supports secure SMTP with SSL/TLS.
β No third-party dependencies added. β Full control over email appearance and content.
Since v2.0.0, LoggerHelper no longer uses Serilog.Sinks.Email. It ships with LoggerHelperEmailSink, allowing:
- β Full HTML customization via external template
- β Dynamic styling based on log level
- β Secure SMTP (SSL/TLS)
Example HTML placeholders:
{{Timestamp}}, {{Level}}, {{Message}}, {{Action}}, {{IdTransaction}}, {{MachineName}}, {{ApplicationName}}, {{LevelClass}}
ποΈ Email Template Customization (optional)
LoggerHelper allows you to customize the HTML structure and appearance of the email body.
You can provide an external .html file with placeholders like:
{{Timestamp}}, {{Level}}, {{Message}}, {{Action}}, {{IdTransaction}}, {{MachineName}}, {{ApplicationName}}, {{LevelClass}}
Then, in the appsettings.LoggerHelper.json configuration file, set:
"LoggerHelper": {
"SerilogOption": {
"Email": {
...
"TemplatePath": "Templates/email-template-default.html"
}
}
}
If the file is missing or invalid, LoggerHelper will fall back to the internal default template, ensuring backward compatibility.
πΎ MS SQL Sink<a id='ms-sql-sink'></a> π
This sink writes logs to a Microsoft SQL Server table and supports additional context properties out of the box.
Configuration Example
"MSSqlServer": {
"connectionString": "<YOUR CONNECTIONSTRING>",
"sinkOptionsSection": {
"tableName": "logs",
"schemaName": "dbo",
"autoCreateSqlTable": true,
"batchPostingLimit": 100,
"period": "0.00:00:10"
},
"columnOptionsSection": {
"addStandardColumns": [
"LogEvent"
],
"removeStandardColumns": [
"Properties"
]
}
}
Explanation
connectionString: Full connection string to the SQL Server instance.tableName: Name of the table that will receive log entries.schemaName: Schema to use for the log table (default isdbo).autoCreateSqlTable: If true, the log table will be created automatically if it does not exist.batchPostingLimit: Number of log events to post in each batch.period: Interval for batching log posts.addStandardColumns: Additional default Serilog columns to include (e.g.,LogEvent).removeStandardColumns: Columns to exclude from the standard set.
Additional Columns
This sink automatically adds the following custom fields to each log:
IdTransaction: a unique identifier for tracking a transaction.MachineName: name of the server or machine.Action: custom action tag if set viaRequest.Action.ApplicationName: name of the application logging the message.
π ElasticSearch Sink<a id='elasticsearch'></a> π
ElasticSearch is ideal for indexing and searching logs at scale. When integrated with Kibana, it enables advanced analytics and visualization of log data.
Benefits
- π Fast full-text search and filtering
- π Seamless integration with Kibana for dashboards
- π Efficient storage and querying for large volumes of structured logs
Example Configuration
"ElasticSearch": {
"nodeUris": "http://<YOUR_IP>:9200",
"indexFormat": "<YOUR_INDEX>"
}
nodeUris: The ElasticSearch node endpoint.indexFormat: The format or name of the index that will store log entries.
π§ͺ Demo API <a id='demo-api'></a> π
Try live with full logging and structured output:
π [Demo Project]
β Now available for both .NET 6.0 and .NET 8.0:
π§° Troubleshooting
Enable Serilog internal diagnostics:
SelfLog.Enable(msg => File.AppendAllText("serilog-selflog.txt", msg));
π€ Author
Alessandro Chiodo π¦ NuGet Package π GitHub
| 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 is compatible. 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. |
-
net6.0
- Microsoft.Extensions.Configuration (>= 9.0.4)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.1)
- Microsoft.Extensions.Configuration.Json (>= 9.0.1)
- Serilog (>= 4.2.0)
- Serilog.Settings.Configuration (>= 9.0.0)
- Serilog.Sinks.Console (>= 6.0.0)
- Serilog.Sinks.Elasticsearch (>= 10.0.0)
- Serilog.Sinks.Email (>= 4.1.0)
- Serilog.Sinks.MSSqlServer (>= 8.1.0)
- Serilog.Sinks.Postgresql.Alternative (>= 3.5.0)
- Serilog.Sinks.Telegram (>= 0.2.1)
- System.Diagnostics.DiagnosticSource (>= 9.0.4)
-
net8.0
- Microsoft.Extensions.Configuration (>= 9.0.4)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.1)
- Microsoft.Extensions.Configuration.Json (>= 9.0.1)
- Serilog (>= 4.2.0)
- Serilog.Settings.Configuration (>= 9.0.0)
- Serilog.Sinks.Console (>= 6.0.0)
- Serilog.Sinks.Elasticsearch (>= 10.0.0)
- Serilog.Sinks.Email (>= 4.1.0)
- Serilog.Sinks.MSSqlServer (>= 8.1.0)
- Serilog.Sinks.Postgresql.Alternative (>= 3.5.0)
- Serilog.Sinks.Telegram (>= 0.2.1)
- System.Diagnostics.DiagnosticSource (>= 9.0.4)
NuGet packages (12)
Showing the top 5 NuGet packages that depend on CSharpEssentials.LoggerHelper:
| Package | Downloads |
|---|---|
|
CSharpEssentials.HttpHelper
package to help manage of httpHelper |
|
|
CSharpEssentials.LoggerHelper.Sink.MSSqlServer
A powerful SQL Server sink for CSharpEssentials.LoggerHelper , designed to log directly into Microsoft SQL Server with automatic table creation, custom columns, and structured context properties. |
|
|
CSharpEssentials.LoggerHelper.Sink.Console
Package Description |
|
|
CSharpEssentials.LoggerHelper.Sink.Elasticsearch
A high-performance Elasticsearch sink for CSharpEssentials.LoggerHelper , designed to index logs into Elasticsearch for fast search, advanced filtering, and real-time dashboards with Kibana. |
|
|
CSharpEssentials.LoggerHelper.Sink.Postgresql
Stores structured logs directly into PostgreSQL with support for custom schemas, JSON fields, and automatic table creation for deep analytics and long-term storage. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.0.10.1 | 188 | 10/12/2025 |
| 4.0.10 | 328 | 10/12/2025 |
| 4.0.9.1 | 113 | 10/10/2025 |
| 4.0.9 | 99 | 10/10/2025 |
| 4.0.8 | 125 | 10/10/2025 |
| 4.0.7 | 163 | 10/8/2025 |
| 4.0.6 | 166 | 10/8/2025 |
| 4.0.5 | 423 | 9/27/2025 |
| 4.0.4 | 110 | 9/27/2025 |
| 4.0.3 | 239 | 9/14/2025 |
| 4.0.2 | 522 | 9/11/2025 |
| 4.0.1.1 | 165 | 9/11/2025 |
| 4.0.1 | 183 | 9/11/2025 |
| 4.0.0 | 842 | 8/25/2025 |
| 3.1.6 | 273 | 8/5/2025 |
| 3.1.5 | 409 | 6/15/2025 |
| 3.1.4 | 451 | 6/12/2025 |
| 3.1.3 | 316 | 6/11/2025 |
| 3.1.2 | 236 | 6/9/2025 |
| 3.1.1 | 614 | 6/8/2025 |
| 3.1.0 | 224 | 6/8/2025 |
| 3.0.6 | 177 | 6/5/2025 |
| 3.0.5 | 176 | 6/5/2025 |
| 3.0.4 | 192 | 6/2/2025 |
| 3.0.3 | 171 | 6/2/2025 |
| 3.0.2 | 604 | 6/1/2025 |
| 3.0.1 | 216 | 5/30/2025 |
| 3.0.0 | 248 | 5/30/2025 |
| 2.0.9 | 103 | 5/24/2025 |
| 2.0.8 | 141 | 5/23/2025 |
| 2.0.7 | 142 | 5/23/2025 |
| 2.0.6 | 161 | 5/19/2025 |
| 2.0.5 | 258 | 5/14/2025 |
| 2.0.4 | 286 | 5/13/2025 |
| 2.0.1 | 182 | 5/11/2025 |
| 2.0.0 | 168 | 5/11/2025 |
| 1.3.1 | 166 | 5/11/2025 |
| 1.2.3 | 118 | 5/10/2025 |
| 1.2.2 | 110 | 5/10/2025 |
| 1.2.1 | 154 | 5/9/2025 |
| 1.1.6 | 181 | 5/6/2025 |
| 1.1.5 | 197 | 5/5/2025 |
| 1.1.4 | 180 | 5/4/2025 |
| 1.1.3 | 193 | 5/4/2025 |
| 1.1.2 | 184 | 5/4/2025 |
| 1.1.1 | 186 | 5/4/2025 |
| 1.0.1 | 149 | 2/19/2025 |
| 1.0.0 | 144 | 2/19/2025 |