https://github.com/andrewkeepcoding/serilog-sinks-memorysink
https://github.com/andrewkeepcoding/serilog-sinks-memorysink
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/andrewkeepcoding/serilog-sinks-memorysink
- Owner: AndrewKeepCoding
- License: mit
- Created: 2024-04-19T16:49:14.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-15T12:30:43.000Z (over 1 year ago)
- Last Synced: 2024-05-16T02:17:04.587Z (over 1 year ago)
- Language: C#
- Size: 96.7 KB
- Stars: 9
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# 🧵 Serilog.Sinks.MemorySink
A [Serilog](https://github.com/serilog/serilog) sink that store log events in memory. You can retrieve these logs and render them in your own log viewer.
The sample app shows how to create a simple log viewer using WinUI 3.

✨**Getting started**
Install *Serilog.Sinks.MemorySink*:
```powershell
dotnet add package Serilog.Sinks.MemorySink
```
Create a logger configuration:
```cs
Log.Logger = new LoggerConfiguration()
.WriteTo.MemorySink(out ILogSource logSource)
.CreateLogger();
```
Retrieve logs:
```cs
var logs = await logSource.GetLogs(startIndex, requiredCount, cancellationToken);
```
⚙️**Custom log item, options and exception handling**
To use a custom log item, you need to provide a log event converter:
```cs
public record LogItem(DateTimeOffset Timestamp, LogEventLevel Level, string Message);
```
```cs
Log.Logger = new LoggerConfiguration()
.WriteTo.MemorySink(
out ILogSource logSource,
options =>
{
options.LogEventConverter = logEvent =>
{
return new LogItem(logEvent.Timestamp, logEvent.Level, logEvent.MessageTemplate.Text);
};
options.MaxLogsCount = 100_000;
options.OnException = ex =>
{
// Handle sink processing exception.
};
})
.CreateLogger();
```