Code
·
53 lines
·
1419 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53# VanDerHeijden.Logging.MongoDb
MongoDB log writer for [VanDerHeijden.Logging](https://www.nuget.org/packages/VanDerHeijden.Logging).
Writes batched log entries to a **MongoDB collection** using `InsertManyAsync`.
## Installation
```bash
dotnet add package VanDerHeijden.Logging.MongoDb
```
## Usage
```csharp
var mongoClient = new MongoClient("mongodb://localhost:27017");
var collection = mongoClient
.GetDatabase("myapp")
.GetCollection<LogEntry>("logs");
builder.Logging.AddMongoDbLogger(collection);
```
## Log entry schema
```csharp
public class LogEntry
{
public string Id { get; set; } // ObjectId
public DateTime Timestamp { get; set; }
public string Level { get; set; }
public string Category { get; set; }
public string Message { get; set; }
public string? Exception { get; set; }
public string? Path { get; set; }
public string? Method { get; set; }
public string? ClientIp { get; set; }
public string? Referer { get; set; }
public string? UserAgent { get; set; }
}
```
The HTTP fields are populated automatically when `IHttpContextAccessor` is registered:
```csharp
builder.Services.AddHttpContextAccessor();
```
Outside an HTTP context they are `null` and not stored in the document.
## Repository
[https://github.com/alphons/VanDerHeijden.Logging](https://github.com/alphons/VanDerHeijden.Logging)