Code
·
61 lines
·
1628 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
54
55
56
57
58
59
60
61# VanDerHeijden.Logging.Sql
SQL Server log writer for [VanDerHeijden.Logging](https://www.nuget.org/packages/VanDerHeijden.Logging).
Writes batched log entries to a **SQL Server table** using `SqlBulkCopy` for high-throughput inserts.
## Installation
```bash
dotnet add package VanDerHeijden.Logging.Sql
```
## Usage
```csharp
builder.Logging.AddSqlLogger(
connectionString: "Server=.;Database=MyApp;Integrated Security=true;",
tableName: "Logs");
```
## Required table schema
```sql
CREATE TABLE Logs (
Id BIGINT IDENTITY PRIMARY KEY,
Timestamp DATETIME2 NOT NULL,
Level NVARCHAR(20) NOT NULL,
Category NVARCHAR(256) NOT NULL,
Message NVARCHAR(MAX) NOT NULL,
Exception NVARCHAR(MAX) NULL,
Path NVARCHAR(1024) NULL,
Method NVARCHAR(10) NULL,
ClientIp NVARCHAR(45) NULL,
Referer NVARCHAR(2048) NULL,
UserAgent NVARCHAR(512) NULL
);
```
The HTTP columns (`Path`, `Method`, `ClientIp`, `Referer`, `UserAgent`) are populated automatically when
`IHttpContextAccessor` is registered in the DI container:
```csharp
builder.Services.AddHttpContextAccessor();
```
Outside an HTTP context (background services, console apps) these columns are `NULL`.
### Migrating an existing table
```sql
ALTER TABLE Logs
ADD Path NVARCHAR(1024) NULL,
Method NVARCHAR(10) NULL,
ClientIp NVARCHAR(45) NULL,
Referer NVARCHAR(2048) NULL,
UserAgent NVARCHAR(512) NULL;
```
## Repository
[https://github.com/alphons/VanDerHeijden.Logging](https://github.com/alphons/VanDerHeijden.Logging)