Logzio.DotNet.NLog
1.2.0
dotnet add package Logzio.DotNet.NLog --version 1.2.0
NuGet\Install-Package Logzio.DotNet.NLog -Version 1.2.0
<PackageReference Include="Logzio.DotNet.NLog" Version="1.2.0" />
paket add Logzio.DotNet.NLog --version 1.2.0
#r "nuget: Logzio.DotNet.NLog, 1.2.0"
// Install Logzio.DotNet.NLog as a Cake Addin #addin nuget:?package=Logzio.DotNet.NLog&version=1.2.0 // Install Logzio.DotNet.NLog as a Cake Tool #tool nuget:?package=Logzio.DotNet.NLog&version=1.2.0
Logz.io NLog Target
Install the NLog target from the Package Manager Console:
Install-Package Logzio.DotNet.NLog
If you prefer to install the library manually, download the latest version from the releases page.
Configuration
XML
If you configure your logging in an XML file, you need to register the assembly and then reference the target.
<nlog>
<extensions>
<add assembly="Logzio.DotNet.NLog"/>
</extensions>
<targets>
<target name="logzio" type="Logzio"
token="<<SHIPPING-TOKEN>>"
logzioType="nlog"
listenerUrl="<<LISTENER-HOST>>:8071"
bufferSize="100"
bufferTimeout="00:00:05"
retriesMaxAttempts="3"
retriesInterval="00:00:02"
includeEventProperties="true"
useGzip="false"
debug="false"
debugLogFile="my_absolute_path\debug.txt"
jsonKeysCamelCase="false"
addTraceContext="false"
>
<contextproperty name="host" layout="${machinename}" />
<contextproperty name="threadid" layout="${threadid}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logzio" />
</rules>
</nlog>
Code
To add the Logz.io target via code, add the following lines:
var config = new LoggingConfiguration();
// Replace these parameters with your configuration
var logzioTarget = new LogzioTarget {
Name = "Logzio",
Token = "<<SHIPPING-TOKEN>>",
LogzioType = "nlog",
ListenerUrl = "<<LISTENER-HOST>>:8071",
BufferSize = 100,
BufferTimeout = TimeSpan.Parse("00:00:05"),
RetriesMaxAttempts = 3,
RetriesInterval = TimeSpan.Parse("00:00:02"),
Debug = false,
DebugLogFile = "my_absolute_path_to_file",
JsonKeysCamelCase = false,
AddTraceContext = false,
// ParseJsonMessage = true,
// ProxyAddress = "http://your.proxy.com:port",
// UseStaticHttpClient = true,
};
config.AddRule(LogLevel.Debug, LogLevel.Fatal, logzioTarget);
LogManager.Configuration = config;
Json Format
To parse your messages as Json add to the logger's configuration the field 'parseJsonMessage' with the value 'true' (or uncomment).
When using 'JsonLayout' set the name of the attribute to other than 'message'.
for example:
<layout type="JsonLayout" includeAllProperties="true">
<attribute name="msg" layout="${message}" encode="false"/>
</layout>
Click here for more information about JsonLayout.
Context Properties
You can configure the target to include your own custom values when forwarding to Logzio. For example:
<nlog>
<variable name="site" value="New Zealand" />
<variable name="rings" value="one" />
<target name="logzio" type="Logzio" token="<<SHIPPING-TOKEN>>" includeEventProperties="true" includeMdlc="false">
<contextproperty name="site" layout="${site}" />
<contextproperty name="rings" layout="${rings}" />
</target>
</nlog>
- includeEventProperties - Include NLog LogEvent Properties. Default=True
- includeMdlc - Include NLog MDLC properties by configuring. Default=False
Notice that the resulting messeage can grow in size to the point where it exceeds the endpoint's capacity. Changing to 'includeEventProperties="false"' will reduce the size of the message being shipped. Alternative you can enable useGzip="true"
.
Extensibility
If you want to change some of the fields or add some of your own, inherit the target and override the ExtendValues
method:
[Target("MyAppLogzio")]
public class MyAppLogzioTarget : LogzioTarget
{
protected override void ExtendValues(LogEventInfo logEvent, Dictionary<string, string> values)
{
values["logger"] = "MyPrefix." + values["logger"];
values["myAppClientId"] = new ClientIdProvider().Get();
}
}
You will then have to change your configuration in order to use your own target.
Trace Context
WARNING: Does not support .NET Standard 1.3
If you’re sending traces with OpenTelemetry instrumentation (auto or manual), you can correlate your logs with the trace context.
In this way, your logs will have traces data in it: span id and trace id.
To enable this feature, set addTraceContext="true"
in your configuration or AddTraceContext = true
in your code (as shown in the previews sections).
Serverless platforms
If you’re using a serverless function, you’ll need to call the appender's flush method at the end of the function run to make sure the logs are sent before the function finishes its execution. You’ll also need to create a static appender in the Startup.cs file so each invocation will use the same appender. The appender should have the UseStaticHttpClient
flag set to true
.
Azure serverless function code sample
Startup.cs
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Logzio.DotNet.NLog;
using NLog;
using NLog.Config;
using System;
[assembly: FunctionsStartup(typeof(LogzioNLogSampleApplication.Startup))]
namespace LogzioNLogSampleApplication
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
var config = new LoggingConfiguration();
// Replace these parameters with your configuration
var logzioTarget = new LogzioTarget
{
Name = "Logzio",
Token = "<<LOG-SHIPPING-TOKEN>>",
LogzioType = "nlog",
ListenerUrl = "https://<<LISTENER-HOST>>:8071",
BufferSize = 100,
BufferTimeout = TimeSpan.Parse("00:00:05"),
RetriesMaxAttempts = 3,
RetriesInterval = TimeSpan.Parse("00:00:02"),
Debug = false,
JsonKeysCamelCase = false,
AddTraceContext = false,
UseStaticHttpClient = true,
// ParseJsonMessage = true,
// ProxyAddress = "http://your.proxy.com:port"
};
config.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Fatal, logzioTarget);
LogManager.Configuration = config;
}
}
}
FunctionApp.cs
using System;
using Microsoft.Azure.WebJobs;
using NLog;
using Microsoft.Extensions.Logging;
using MicrosoftLogger = Microsoft.Extensions.Logging.ILogger;
namespace LogzioNLogSampleApplication
{
public class TimerTriggerCSharpNLog
{
private static readonly Logger nLog = LogManager.GetCurrentClassLogger();
[FunctionName("TimerTriggerCSharpNLog")]
public void Run([TimerTrigger("*/30 * * * * *")]TimerInfo myTimer, MicrosoftLogger msLog)
{
msLog.LogInformation($"NLogzio C# Timer trigger function executed at: {DateTime.Now}");
nLog.WithProperty("iCanBe", "your long lost pal")
.WithProperty("iCanCallYou", "Betty, and Betty when you call me")
.WithProperty("youCanCallMe", "Al")
.Info("If you'll be my bodyguard");
// Call Flush method before function trigger finishes
LogManager.Flush(5000);
}
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard1.3 is compatible. netstandard1.4 was computed. netstandard1.5 was computed. netstandard1.6 was computed. netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Universal Windows Platform | uap was computed. uap10.0 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 3.1
- Newtonsoft.Json (>= 13.0.1)
- NLog (>= 4.7.11)
- OpenTelemetry (>= 1.3.1)
-
.NETStandard 1.3
- NETStandard.Library (>= 1.6.1)
- Newtonsoft.Json (>= 13.0.1)
- NLog (>= 4.7.11)
-
.NETStandard 2.0
- Newtonsoft.Json (>= 13.0.1)
- NLog (>= 4.7.11)
- OpenTelemetry (>= 1.3.1)
-
net5.0
- Newtonsoft.Json (>= 13.0.1)
- NLog (>= 4.7.11)
- OpenTelemetry (>= 1.3.1)
-
net6.0
- Newtonsoft.Json (>= 13.0.1)
- NLog (>= 4.7.11)
- OpenTelemetry (>= 1.3.1)
-
net8.0
- Newtonsoft.Json (>= 13.0.1)
- NLog (>= 4.7.11)
- OpenTelemetry (>= 1.8.1)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Logzio.DotNet.NLog:
Package | Downloads |
---|---|
LagoVista.IoT.Logging
Provides Logging and Error Reporing Implementations for the LagoVista Devices, IoT and Home Automation Frameworks. |
|
Logzio.NLog.Extensions
A logging extension to simplify configuration of Logzio's NLog package for sending log entries to Logz.io |
|
Crossroads.Microservice.Logging
C# .Net library for boostrapping logging to align with Crossroads best practices. |
|
Crossroads.Microservice.Logging.Upgraded
C# .Net library for boostrapping logging to align with Crossroads best practices. Upgrade to .NET 6 |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Logzio.DotNet.NLog:
Repository | Stars |
---|---|
Jericho/StrongGrid
Strongly typed library for the entire SendGrid v3 API, including webhooks
|
Version | Downloads | Last updated |
---|---|---|
1.2.0 | 24,323 | 5/27/2024 |
1.1.1 | 7,073 | 5/20/2024 |
1.1.0 | 63,460 | 10/23/2023 |
1.0.16 | 78,927 | 2/26/2023 |
1.0.15 | 6,806 | 2/15/2023 |
1.0.14 | 2,750 | 1/31/2023 |
1.0.13 | 273,494 | 9/14/2021 |
1.0.12 | 41,098 | 7/27/2021 |
1.0.11 | 22,558 | 5/24/2021 |
1.0.10 | 241,265 | 5/12/2020 |
1.0.9 | 27,701 | 2/5/2020 |
1.0.8 | 4,837 | 1/12/2020 |
1.0.7 | 299,320 | 9/8/2019 |
1.0.6 | 46,038 | 3/5/2019 |
1.0.5 | 729 | 2/24/2019 |
1.0.4 | 25,662 | 11/19/2018 |
1.0.3 | 34,269 | 9/3/2018 |
1.0.0 | 1,057 | 8/21/2018 |
0.21.0 | 3,356 | 8/7/2018 |
0.20.0 | 53,068 | 1/17/2017 |
0.3.0 | 850 | 8/21/2018 |
0.1.0 | 1,075 | 12/21/2016 |
Option to format message as json