Configure log4net
Before you begin, you’ll need:
- log4net 2.0.8 or higher
- .NET Core SDK version 2.0 or higher
- .NET Framework version 4.6.1 or higher
Add the dependency to your project
If you’re on Windows, navigate to your project’s folder in the command line, and run this command to install the dependency.
Install-Package Logzio.DotNet.Log4net
If you’re on a Mac or Linux machine, you can install the package using Visual Studio. Select Project > Add NuGet Packages..., and then search for Logzio.DotNet.Log4net.
Configure the appender
You can configure the appender in a configuration file or directly in the code. Use the samples in the code blocks below as a starting point, and replace them with a configuration that matches your needs. See log4net documentation 🔗 to learn more about configuration options.
For a complete list of options, see the configuration parameters below the code blocks.👇
Option 1: In a configuration file
<log4net>
<appender name="LogzioAppender" type="Logzio.DotNet.Log4net.LogzioAppender, Logzio.DotNet.Log4net">
<!-- Replace these parameters with your configuration -->
<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>
<jsonKeysCamelCase>false</jsonKeysCamelCase>
<!--<parseJsonMessage>true</parseJsonMessage>-->
</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.
Option 2: In the code
var hierarchy = (Hierarchy)LogManager.GetRepository();
var logzioAppender = new LogzioAppender();
// Replace these parameters with your configuration
logzioAppender.AddToken("<<LOG-SHIPPING-TOKEN>>");
logzioAppender.AddType("log4net");
logzioAppender.AddListenerUrl("https://<<LISTENER-HOST>>:8071");
logzioAppender.AddBufferSize(100);
logzioAppender.AddBufferTimeout(TimeSpan.FromSeconds(5));
logzioAppender.AddRetriesMaxAttempts(3);
logzioAppender.AddRetriesInterval(TimeSpan.FromSeconds(2));
logzioAppender.AddDebug(false);
logzioAppender.AddGzip(true);
logzioAppender.JsonKeysCamelCase(false);
// <-- Uncomment and edit this line to enable proxy routing: -->
// logzioAppender.AddProxyAddress("http://your.proxy.com:port");
// <-- Uncomment this to parse messages as JSON -->
// logzioAppender.ParseJsonMessage(true);
hierarchy.Root.AddAppender(logzioAppender);
hierarchy.Configured = true;
Parameters
| Parameter | Description | Default/Required |
|---|---|---|
| token | Your Layerlog log shipping token securely directs the data to your Layerlog account. Replace <<LOG-SHIPPING-TOKEN>> with the token of the account you want to ship to. |
Required |
| listenerUrl | Listener URL and port. Replace <<LISTENER-HOST>> with the host for your region. For example, listener.layerlog.com if your account is hosted on AWS US East, or listener-nl.layerlog.com if hosted on Azure West Europe. |
https://listener.layerlog.com:8071 |
| type | The log type, shipped as type field. Used by Layerlog for consistent parsing. Can’t contain spaces. |
log4net |
| bufferSize | Maximum number of messages the logger will accumulate before sending them all as a bulk. | 100 |
| bufferTimeout | Maximum time to wait for more log lines, as hh:mm:ss.fff. | 00:00:05 |
| retriesMaxAttempts | Maximum number of attempts to connect to Layerlog. | 3 |
| retriesInterval | Time to wait between retries, as hh:mm:ss.fff. | 00:00:02 |
| gzip | To compress the data before shipping, true. Otherwise, false. |
false |
| debug | To print debug messages to the console and trace log, true. Otherwise, false. |
false |
| parseJsonMessage | To parse your message as JSON format, add this field and set it to true. |
false |
| proxyAddress | Proxy address to route your logs through. | None |
| jsonKeysCamelCase | If you have custom fields keys that start with a capital letter and want to see the fields with a capital letter in Layerlog, set this field to true. | false |
Code sample
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();
}
}
}
Custom fields
You can add static keys and values to be added to all log messages.
These custom fields must be children of <appender>, as shown here.
<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>
Extending the appender
To change or add fields to your logs, 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();
}
}
Change your configuration to use your new appender name.
For the example above, you’d use MyAppLogzioAppender.