Logzio.DotNet.Log4net
1.2.0
dotnet add package Logzio.DotNet.Log4net --version 1.2.0
NuGet\Install-Package Logzio.DotNet.Log4net -Version 1.2.0
<PackageReference Include="Logzio.DotNet.Log4net" Version="1.2.0" />
paket add Logzio.DotNet.Log4net --version 1.2.0
#r "nuget: Logzio.DotNet.Log4net, 1.2.0"
// Install Logzio.DotNet.Log4net as a Cake Addin #addin nuget:?package=Logzio.DotNet.Log4net&version=1.2.0 // Install Logzio.DotNet.Log4net as a Cake Tool #tool nuget:?package=Logzio.DotNet.Log4net&version=1.2.0
Logz.io log4net Appender
Install the log4net appender from the Package Manager Console:
Install-Package Logzio.DotNet.Log4net
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, simply add a reference to the Logz.io appender.
<log4net>
<appender name="LogzioAppender" type="Logzio.DotNet.Log4net.LogzioAppender, Logzio.DotNet.Log4net">
<token><<LOG-SHIPPING-TOKEN>></token>
<type>log4net</type>
<listenerUrl>https://<<LISTENER-HOST>>:8071</listenerUrl>
<bufferSize>100</bufferSize>
<bufferTimeout>00:00:05</bufferTimeout>
<retriesMaxAttempts>3</retriesMaxAttempts>
<retriesInterval>00:00:02</retriesInterval>
<gzip>true</gzip>
<debug>false</debug>
<debugLogFile>my_absolute_path_to_file</debugLogFile>
<jsonKeysCamelCase>false</jsonKeysCamelCase>
<addTraceContext>false</addTraceContext>
<UseStaticHttpClient>false</UseStaticHttpClient>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="LogzioAppender" />
</root>
</log4net>
Add a reference to the configuration file in your code, as shown in the example here.
Code
To add the Logz.io appender via code, add the following lines:
var hierarchy = (Hierarchy)LogManager.GetRepository();
var logzioAppender = new LogzioAppender();
logzioAppender.AddToken("<<LOG-SHIPPING-TOKEN>>");
logzioAppender.AddListenerUrl("<<LISTENER-HOST>>");
// <-- Uncomment and edit this line to enable proxy routing: -->
// logzioAppender.AddProxyAddress("http://your.proxy.com:port");
// <-- Uncomment this to enable sending logs in Json format -->
// logzioAppender.ParseJsonMessage(true);
// <-- Uncomment these lines to enable gzip compression -->
// logzioAppender.AddGzip(true);
// logzioAppender.ActivateOptions();
// logzioAppender.JsonKeysCamelCase(false);
// logzioAppender.AddTraceContext(false);
// logzioAppender.AddDebug(false);
// logzioAppender.AddDebugLogFile("my_absolute_path_to_file");
// logzioAppender.UseStaticHttpClient(false);
logzioAppender.ActivateOptions();
hierarchy.Root.AddAppender(logzioAppender);
hierarchy.Root.Level = Level.All;
hierarchy.Configured = true;
Custom Fields
You can add static keys and values to be added to all log messages. For example:
<appender name="LogzioAppender" type="Logzio.DotNet.Log4net.LogzioAppender, Logzio.DotNet.Log4net">
<customField>
<key>Environment</key>
<value>Production</value>
</customField>
<customField>
<key>Location</key>
<value>New Jerseay B1</value>
</customField>
</appender>
Extensibility
If you want to change some of the fields or add some of your own, inherit the appender and override the ExtendValues
method:
public class MyAppLogzioAppender : LogzioAppender
{
protected override void ExtendValues(LoggingEvent loggingEvent, 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 appender.
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</addTraceContext>
in your configuration or logzioAppender.AddTraceContext(true);
in your code (as shown in the previews sections).
Code Samples
XML
using System.IO;
using log4net;
using log4net.Config;
using System.Reflection;
namespace dotnet_log4net
{
class Program
{
static void Main(string[] args)
{
var logger = LogManager.GetLogger(typeof(Program));
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
// Replace "App.config" with the config file that holds your log4net configuration
XmlConfigurator.Configure(logRepository, new FileInfo("App.config"));
logger.Info("Now I don't blame him 'cause he run and hid");
logger.Info("But the meanest thing he ever did");
logger.Info("Before he left was he went and named me Sue");
LogManager.Shutdown();
}
}
}
Code
using log4net;
using log4net.Core;
using log4net.Repository.Hierarchy;
using Logzio.DotNet.Log4net;
namespace dotnet_log4net
{
class Program
{
static void Main(string[] args)
{
var hierarchy = (Hierarchy)LogManager.GetRepository();
var logger = LogManager.GetLogger(typeof(Program));
var logzioAppender = new LogzioAppender();
logzioAppender.AddToken("<<LOG-SHIPPING-TOKEN>>");
logzioAppender.AddListenerUrl("https://<<LISTENER-HOST>>:8071");
// <-- Uncomment and edit this line to enable proxy routing: -->
// logzioAppender.AddProxyAddress("http://your.proxy.com:port");
// <-- Uncomment this to enable sending logs in Json format -->
// logzioAppender.ParseJsonMessage(true);
// <-- Uncomment these lines to enable gzip compression -->
// logzioAppender.AddGzip(true);
// logzioAppender.ActivateOptions();
// logzioAppender.JsonKeysCamelCase(false)
// logzioAppender.AddTraceContext(false);
// logzioAppender.AddDebug(false);
// logzioAppender.AddDebugLogFile("my_absolute_path_to_file");
// logzioAppender.UseStaticHttpClient(false);
logzioAppender.ActivateOptions();
hierarchy.Root.AddAppender(logzioAppender);
hierarchy.Configured = true;
hierarchy.Root.Level = Level.All;
logger.Info("Now I don't blame him 'cause he run and hid");
logger.Info("But the meanest thing he ever did");
logger.Info("Before he left was he went and named me Sue");
LogManager.Shutdown();
}
}
}
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 log4net;
using log4net.Repository.Hierarchy;
using Logzio.DotNet.Log4net;
[assembly: FunctionsStartup(typeof(LogzioLog4NetSampleApplication.Startup))]
namespace LogzioLog4NetSampleApplication
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
var hierarchy = (Hierarchy)LogManager.GetRepository();
var logzioAppender = new LogzioAppender();
logzioAppender.AddToken("<<LOG-SHIPPING-TOKEN>>");
logzioAppender.AddListenerUrl("https://<<LISTENER-HOST>>:8071");
logzioAppender.ActivateOptions();
logzioAppender.UseStaticHttpClient(true);
hierarchy.Root.AddAppender(logzioAppender);
hierarchy.Configured = true;
}
}
}
FunctionApp.cs
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using log4net;
using MicrosoftLogger = Microsoft.Extensions.Logging.ILogger;
namespace LogzioLog4NetSampleApplication
{
public class TimerTriggerCSharpLog4Net
{
private static readonly ILog logger = LogManager.GetLogger(typeof(TimerTriggerCSharpLog4Net));
[FunctionName("TimerTriggerCSharpLog4Net")]
public void Run([TimerTrigger("*/30 * * * * *")]TimerInfo myTimer, MicrosoftLogger msLog)
{
msLog.LogInformation($"Log4Net C# Timer trigger function executed at: {DateTime.Now}");
logger.Info("Now I don't blame him 'cause he run and hid");
logger.Info("But the meanest thing he ever did");
logger.Info("Before he left was he went and named me Sue");
LogManager.Flush(5000);
msLog.LogInformation($"Log4Net C# Timer trigger function finished at: {DateTime.Now}");
}
}
}
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
- log4net (>= 2.0.12)
- Newtonsoft.Json (>= 13.0.1)
- OpenTelemetry (>= 1.3.1)
-
.NETStandard 1.3
- log4net (>= 2.0.12)
- NETStandard.Library (>= 1.6.1)
- Newtonsoft.Json (>= 13.0.1)
-
.NETStandard 2.0
- log4net (>= 2.0.12)
- Newtonsoft.Json (>= 13.0.1)
- OpenTelemetry (>= 1.3.1)
-
net5.0
- log4net (>= 2.0.12)
- Newtonsoft.Json (>= 13.0.1)
- OpenTelemetry (>= 1.3.1)
-
net6.0
- log4net (>= 2.0.12)
- Newtonsoft.Json (>= 13.0.1)
- OpenTelemetry (>= 1.3.1)
-
net8.0
- log4net (>= 2.0.12)
- Newtonsoft.Json (>= 13.0.1)
- OpenTelemetry (>= 1.8.1)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Logzio.DotNet.Log4net:
Package | Downloads |
---|---|
EPost.PrimitiveTypes.AspNetCore.Utils
EPost PrimitiveTypes AspNetCore Utils |
|
Bainkom.PrimitiveTypes.AspNetCore.Utils
Bainkom AspNetCore Utils |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.2.0 | 2,215 | 5/27/2024 |
1.1.1 | 732 | 5/20/2024 |
1.1.0 | 24,984 | 10/23/2023 |
1.0.16 | 17,163 | 2/26/2023 |
1.0.15 | 1,106 | 2/15/2023 |
1.0.14 | 1,747 | 1/31/2023 |
1.0.13 | 181,788 | 9/14/2021 |
1.0.12 | 4,685 | 7/27/2021 |
1.0.11 | 8,939 | 5/24/2021 |
1.0.10 | 68,476 | 5/12/2020 |
1.0.9 | 4,558 | 2/16/2020 |
1.0.8 | 2,090 | 1/12/2020 |
1.0.7 | 50,212 | 9/8/2019 |
1.0.6 | 9,191 | 3/5/2019 |
1.0.5 | 721 | 2/24/2019 |
1.0.4 | 10,235 | 11/19/2018 |
1.0.3 | 6,990 | 9/3/2018 |
1.0.0 | 1,003 | 8/21/2018 |
0.21.0 | 1,172 | 8/7/2018 |
0.20.0 | 20,357 | 1/17/2017 |
0.3.0 | 994 | 8/21/2018 |
0.1.0 | 1,180 | 12/21/2016 |
Option to format message as json