Skip to content

Commit a654e6e

Browse files
authored
Update send-logs-from-dotnet.mdx (#235)
1 parent 70a7e41 commit a654e6e

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

guides/send-logs-from-dotnet.mdx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,13 @@ This command builds the project and runs the app. You see the log messages being
210210

211211
### Install Serilog Packages
212212

213-
Add Serilog and the necessary extensions to your project. You need the `Serilog`, `Serilog.Sinks.Http`, and `Serilog.Formatting.Json` packages.
213+
Add Serilog and the necessary extensions to your project. You need the `Serilog`, `Serilog.Sinks.Http`, `Serilog.Formatting.Elasticsearch` and `Serilog.Formatting.Json` packages.
214214

215215
```bash
216216
dotnet add package Serilog
217217
dotnet add package Serilog.Sinks.Http
218218
dotnet add package Serilog.Formatting.Json
219+
dotnet add package Serilog.Formatting.Elasticsearch
219220
```
220221

221222
### Configure Serilog
@@ -224,11 +225,12 @@ In your `Program.cs` or a startup configuration file, set up Serilog to use the
224225

225226
```csharp
226227
using Serilog;
228+
using Serilog.Formatting.Elasticsearch;
227229

228230
Log.Logger = new LoggerConfiguration()
229231
.WriteTo.Http(
230232
requestUri: "https://api.axiom.co/v1/datasets/YOUR-DATASET-NAME/ingest",
231-
textFormatter: new Serilog.Formatting.Json.JsonFormatter(),
233+
textFormatter: new ElasticsearchJsonFormatter(renderMessageTemplate: false, inlineFields: true),
232234
httpClient: new HttpClient
233235
{
234236
DefaultRequestHeaders =
@@ -301,10 +303,10 @@ Ensure your `axiomlogs.csproj` file is configured with the package references. T
301303
<PackageReference Include="Serilog" Version="2.10.0" />
302304
<PackageReference Include="Serilog.Sinks.Http" Version="5.0.0" />
303305
<PackageReference Include="Serilog.Formatting.Json" Version="3.1.0" />
306+
<PackageReference Include="Serilog.Formatting.Elasticsearch" Version="8.4.1" />
304307
</ItemGroup>
305308

306309
</Project>
307-
308310
```
309311

310312
### Build and run the app
@@ -327,6 +329,7 @@ You need NLog and potentially an extension for HTTP targets.
327329
```bash
328330
dotnet add package NLog
329331
dotnet add package NLog.Web.AspNetCore
332+
dotnet add package NLog.Targets.Http
330333
```
331334

332335
### Configure NLog
@@ -336,16 +339,25 @@ Set up NLog by creating an `NLog.config` file or configuring it programmatically
336339
```xml
337340
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
338341
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
342+
<extensions>
343+
<add assembly="NLog.Targets.Http" />
344+
</extensions>
339345
<targets>
340346
<target xsi:type="BufferingWrapper"
341347
name="allLogs"
342348
flushTimeout="5000">
343-
<target xsi:type="WebClient"
344-
name="http"
349+
<target xsi:type="HTTP"
350+
name="axiom"
345351
url="https://api.axiom.co/v1/datasets/YOUR-DATASET-NAME/ingest"
346-
method="POST"
347-
header="Authorization: Bearer YOUR-API-TOKEN"
348-
layout="${longdate}|${level:uppercase=true}|${message} ${exception:format=toString,Data}">
352+
HttpHeaders="Authorization: Bearer YOUR-API-TOKEN"
353+
contentType="application/json">
354+
<layout xsi:type="JsonLayout" includeAllProperties="true">
355+
<attribute name="timestamp" layout="${date:universalTime=true:format=o}" />
356+
<attribute name="level" layout="${level:lowercase=true}" />
357+
<attribute name="message" layout="${message}" />
358+
<attribute name="exception" layout="${exception:format=toString}"
359+
encode="false" />
360+
</layout>
349361
</target>
350362
</target>
351363
</targets>
@@ -427,6 +439,7 @@ Ensure your `axiomlogs.csproj` file is configured with the package references. T
427439
<ItemGroup>
428440
<PackageReference Include="NLog" Version="4.7.12" />
429441
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.3" />
442+
<PackageReference Include="NLog.Targets.Http" Version="1.0.4" />
430443
</ItemGroup>
431444

432445
</Project>

0 commit comments

Comments
 (0)