Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/serilog-contrib/Serilog.Sinks.DelegatingText
A Serilog sink to emit formatted log events to a delegate.
https://github.com/serilog-contrib/Serilog.Sinks.DelegatingText
Last synced: 3 months ago
JSON representation
A Serilog sink to emit formatted log events to a delegate.
- Host: GitHub
- URL: https://github.com/serilog-contrib/Serilog.Sinks.DelegatingText
- Owner: serilog-contrib
- Created: 2021-06-26T07:10:09.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-09-04T17:27:40.000Z (over 1 year ago)
- Last Synced: 2024-10-03T17:04:36.320Z (4 months ago)
- Language: C#
- Homepage:
- Size: 85.9 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-m-files - Serilog.Sinks.DelegatingText - A Serilog sink for writing to a delegate (used in a vault application for a to-be-published Serilog sink) (Development / NuGET Packages)
README
# Serilog.Sinks.DelegatingText
[![Nuget status](https://img.shields.io/nuget/v/Serilog.Sinks.DelegatingText.svg)](https://www.nuget.org/packages/Serilog.Sinks.DelegatingText)
A Serilog sink wrapper to write formatted log events to a delegate.
To use the sink, install the **Serilog.Sinks.DelegatingText nupkg** into your solution; see below or browse to the [sample sandbox code in this repository](src/SANDBOX/Program.cs) for pointers how to use it. And here is the nuget.org page for [Serilog.Sinks.DelegatingText](https://www.nuget.org/packages/Serilog.Sinks.DelegatingText/).
*Please note that this library is provided "as-is" and with no warranty, explicit or otherwise. You should ensure that the functionality meets your requirements, and thoroughly test them, prior to using in any production scenarios.*
## Use case
The **Serilog.Sinks.DelegatingText** sink makes it possible buffer formatted log events in your application. For more information about Serilog, see [Serilog.net](https://serilog.net/) and [https://github.com/serilog/serilog](https://github.com/serilog/serilog).
```csharp
private readonly StringBuilder _logEventBuffer = new StringBuilder();private void ConfigureApp()
{
Log.Logger = new LoggerConfiguration()
// Using a delegate to buffer log messages
.WriteTo.DelegatingTextSink(w => WriteToBuffer(w), outputTemplate:"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
.CreateLogger();Log.Information("Hello from the sandbox");
Log.Warning("Sample warning");
Log.Error("Sample error");
Log.Error(new Exception("A sample exception"), "Sample error with exception");// ...
}private void WriteToBuffer(string formattedLogMessage)
{
_logEventBuffer.AppendLine(formattedLogMessage.TrimEnd(Environment.NewLine.ToCharArray()));
}private void SomeFunction()
{
// do something with the buffered log events
_logEventBuffer.ToString();
}
```