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"
#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
TraceAsync
onfinally
block ofRequestResponseLoggingMiddleware
- 1.1.6 β Fixed issues detected by CodeQL
- 1.2.1 β Optimized with test Web API
- 1.2.2 β Optimized
Properties
handling 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
IRequest
interface - 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
SerilogCondition
for each sink with the desiredLevel
- If
Level
is 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
ColumnsPostGreSQL
is not set, the following default columns will be created and used:message
,message_template
,level
,raise_date
,exception
,properties
,props_test
,machine_name
If
ColumnsPostGreSQL
is defined, LoggerHelper will use the exact fields provided.Setting
addAutoIncrementColumn: true
will add anid SERIAL PRIMARY KEY
automatically.
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 enableSelfLog
during 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
Error
orFatal
to 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 (8)
Showing the top 5 NuGet packages that depend on CSharpEssentials.LoggerHelper:
Package | Downloads |
---|---|
CSharpEssentials.LoggerHelper.Sink.Postgresql
Package Description |
|
CSharpEssentials.LoggerHelper.Sink.File
Sink File for package LoggerHelper |
|
CSharpEssentials.LoggerHelper.Sink.Elasticsearch
Sink ElasticSearch for CSharpEssentials.LoggerHelper |
|
CSharpEssentials.LoggerHelper.Sink.MSSqlServer
Package Description |
|
CSharpEssentials.LoggerHelper.Sink.Console
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.1.5 | 137 | 5 days ago |
3.1.4 | 327 | 8 days ago |
3.1.3 | 273 | 9 days ago |
3.1.2 | 196 | 11 days ago |
3.1.1 | 393 | 11 days ago |
3.1.0 | 176 | 11 days ago |
3.0.6 | 137 | 15 days ago |
3.0.5 | 130 | 15 days ago |
3.0.4 | 127 | 17 days ago |
3.0.3 | 129 | 17 days ago |
3.0.2 | 335 | 19 days ago |
3.0.1 | 133 | 21 days ago |
3.0.0 | 143 | 21 days ago |
2.0.9 | 62 | a month ago |
2.0.8 | 97 | a month ago |
2.0.7 | 97 | a month ago |
2.0.6 | 129 | a month ago |
2.0.5 | 213 | a month ago |
2.0.4 | 239 | a month ago |
2.0.1 | 121 | a month ago |
2.0.0 | 122 | a month ago |
1.3.1 | 124 | a month ago |
1.2.3 | 71 | a month ago |
1.2.2 | 59 | a month ago |
1.2.1 | 105 | a month ago |
1.1.6 | 134 | a month ago |
1.1.5 | 153 | a month ago |
1.1.4 | 139 | 2 months ago |
1.1.3 | 142 | 2 months ago |
1.1.2 | 141 | 2 months ago |
1.1.1 | 133 | 2 months ago |
1.0.1 | 109 | 4 months ago |
1.0.0 | 94 | 4 months ago |