---
title: Implement Log4Net Appender in a New .NET Application
description: Learn how to implement the Nodinite Log4Net Appender in a new .NET application. This guide covers step-by-step integration, configuration, and advanced logging features for seamless monitoring and troubleshooting.
---

# How to implement Log4Net in a new .NET Application

Easily integrate the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [Log4Net Appender][] into your .NET applications for advanced, centralized logging and monitoring. This guide walks you through every step, from NuGet installation to advanced configuration, so you can:

* ✅ Achieve seamless log integration with <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span>  
* ✅ Leverage advanced configuration and override options  
* ✅ Enable robust monitoring and troubleshooting  
* ✅ Support both .NET Framework and modern .NET projects  

<i class="fal fa-arrow-down-to-square" aria-hidden="true"></i> [Package on **NuGet.org**](https://www.nuget.org/packages/Nodinite.LogAgent.Log4NetAppender)<sup><i class="fal fa-arrow-up-right-from-square" aria-hidden="true"></i></sup>

<aside aria-label="Info" class="nodinite-note nodinite-note--info">
<p class="nodinite-note__label" aria-hidden="true"><i class="far fa-fw fa-square-info"></i> Info</p>
<div class="nodinite-note__content"><p>This page describes the steps about how to add the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> <a href="https://docs.nodinite.com/logging-and-monitoring/log4net-appender/overview/">Log4Net Appender</a> to your Visual Studio Project and Log events using the <a href="https://logging.apache.org/log4net/index.html">Log4Net</a> framework to <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span>.</p></div>
</aside>

<aside aria-label="Tip" class="nodinite-note nodinite-note--tip">
<p class="nodinite-note__label" aria-hidden="true"><i class="far fa-fw fa-lightbulb"></i> Tip</p>
<div class="nodinite-note__content"><p>You should use the <a href="https://docs.nodinite.com/logging-and-monitoring/serilog/overview/">Serilog</a> if you are using .NET.</p></div>
</aside>

## <i class="fal fa-forward-step" aria-hidden="true"></i> Step 1. Add package reference

Start by adding the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> Log4Net Nuget Package to your Visual Studio project.  
![Add NuGet Package Reference][1]
*Add the Nodinite Log4Net Appender NuGet package to your Visual Studio project.*

## <i class="fal fa-forward-step" aria-hidden="true"></i> Step 2. Create handle

Then, add a variable to acquire a handle to the Log4Net instance:  

```csharp
var log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 
```

*.NET Framework.*

## <i class="fal fa-forward-step" aria-hidden="true"></i> Step 3. Include namespace

Resolve the issue with the missing namespace by adding a **using** declaration at the top of the Class:

```csharp
using log4net;
```

*.NET Framework.*

## <i class="fal fa-forward-step" aria-hidden="true"></i> Step 4. Add the Log4Net configuration file

Add the `log4net.config` file, make sure to set **Copy to Output Directory** to `Copy Always`.

<aside aria-label="Tip" class="nodinite-note nodinite-note--tip">
<p class="nodinite-note__label" aria-hidden="true"><i class="far fa-fw fa-lightbulb"></i> Tip</p>
<div class="nodinite-note__content"><p>You can <a href="#override-configuration-values">override</a> the configuration values in code!</p></div>
</aside>

## <i class="fal fa-forward-step" aria-hidden="true"></i> Step 5. Manage Log4Net configuration

Add the following default XML configuration to the `log4net.config` file (Change values to your liking or even better, [overide](#override-configuration-values) the run-time values in code).  

The fields map 1:1 with the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [JSON Log Event][]. LogText (Exception object) and LogStatus are implicitly provided in your code.

<aside aria-label="Note" class="nodinite-note nodinite-note--note">
<p class="nodinite-note__label" aria-hidden="true"><i class="far fa-fw fa-note"></i> Note</p>
<div class="nodinite-note__content"><p>The message param is the <strong>Body</strong> (Payload) in <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span>.</p></div>
</aside>

```xml
    <log4net>
    <appender name="Log4NetAppender" type="Nodinite.LogAgent.Log4NetAppender.NodiniteLog4NetAppender,Nodinite.LogAgent.Log4NetAppender">
        <!-- Custom Parameters -->
        <OriginalMessageType value="Nodinite.LogAgent.Log4NetAppender/2.0#DefaultMessageType" />
        <MessageTypeExtractFromBody value="false" />
        <LogAgentID value="101"/>
        <EventNumber value="0"/>
        <EventDirection value="66">
        <EndPointDirection value="-2">
        <EndPointName value="Log4Net Unit Test"/>
        <EndPointUri value="VS.local.log4net.test"/>
        <!--<LogText value =""/>--> <!-- Should not be part of the configuration file -->
        <!--<LogStatusId value ="255"/>--> <!-- Should not be part of the configuration file -->
        <ProcessingUser value="Administrator"/>
        <ModuleType value="unit test"/>
        <ProcessName value="unit test"/>
        <ProcessingMachineName value="DEV"/>
        <ProcessingModuleType value="unit test"/>
        <ProcessingModuleName value="VS"/>
        <LogApiServiceURI value="https://localhost:40002/LogAPI/"/>
        <ApplicationInterchangeId value="RUN-123">
        <LocalInterchangeId value="{E4CDDF18-9925-4A79-8E9F-71BC0D4C5172}"/>
        <ServiceInstanceActivityId value="{E4CDDF18-9925-4A79-8E9F-71BC0D4C5173}"/>
    </appender>
    <root>
        <level value="DEBUG"/>
        <appender-ref ref="Log4NetAppender"/>
    </root>
</log4net>
```

<aside aria-label="Important" class="nodinite-note nodinite-note--important">
<p class="nodinite-note__label" aria-hidden="true"><i class="far fa-fw fa-message-exclamation"></i> Important</p>
<div class="nodinite-note__content"><p>Ensure the <strong>LogApiServiceURI</strong> has a valid address (URI) to the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> <a href="https://docs.nodinite.com/core-services/log-api/overview/">Log API</a> (the host and port come from your environment's settings in the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> Portal (<a href="https://docs.nodinite.com/install-and-update/getting-started/version-7/install-nodinite-v7-steps/04-servers/#core-platform-services">Servers step</a>)) OR has a folder that exist on disk. Make sure the path ends with a trailing backslash <code>\</code>.</p></div>
</aside>

### LogApiServiceURI

||LogApiServiceURI|Comment|  
|---|---|---|  
|<i class="fal fa-globe" aria-hidden="true"></i> [Log API][]|`https://localhost:40002/LogAPI/`|**NOTE:** The call is **synchronous**, hence, if service is unavailable, you will lose the log event|  
|<i class="fal fa-folder" aria-hidden="true"></i> Folder <sup><span class="nodinite-new-badge">New 6.0.8</span></sup>|`c:\temp\`|Ensure the target folder is highly available. Use the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [Pickup Log Events Service Logging Agent][Pickup Service] to consume the files, and use the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [File Monitoring Agent][] to ensure the files do not stockpile|  

### What's New in Version 6.0.8

<sup><span class="nodinite-new-badge">New 6.0.8</span></sup>

* `LogText` can be set unless you are logging an Exception.  
  <aside aria-label="Note" class="nodinite-note nodinite-note--note">
  <p class="nodinite-note__label" aria-hidden="true"><i class="far fa-fw fa-note"></i> Note</p>
  <div class="nodinite-note__content"><p>Internally, the <code>LogText</code> is set to null on each Log. You must set it again for each event to be logged.</p></div>
  </aside>

* `ApplicationInterchangeId` can be a string.  
* [EndPointDirection][Endpoint Directions] can be set, the default is "None (-2)".
* [EventDirection][EventDirections] can be set, default is "Process Outgoing (66)".
* All config values except `MessageTypeExtractFromBody` can be [overriden in code](#override-configuration-values).  
* [LogApiServiceURI](#logapiserviceuri) can either be a folder, or an address to the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [Log API][].  
* `SequenceNo` can be set programmatically (should not be set in config file).  

### What's New in Version 6.0.0.9

<sup><span class="nodinite-new-badge">New 6.0.0.9</span></sup>

You can set the [Log4Net Appender][] to legacy mode, by default, this option is turned off. We built the Logger to Log Business Transactions. If you set this property to `true`, then `Hello World` is in the Log Text field if your code is:  `_logger.LogInformation("Hello World");`.

This is a new property to set in the config file

```xml
<LogMessageObjectAsLogText value="true"/>
```

*Set the `LogMessageObjectAsLogText` to `true` to enable legacy mode.*

<aside aria-label="Note" class="nodinite-note nodinite-note--note">
<p class="nodinite-note__label" aria-hidden="true"><i class="far fa-fw fa-note"></i> Note</p>
<div class="nodinite-note__content"><p>This settings also affects logging errors!</p></div>
</aside>

## <i class="fal fa-forward-step" aria-hidden="true"></i> Step 6. Add Watcher

Add the line: `[assembly: log4net.Config.XmlConfigurator(Watch = true)]` to the **Properties.cs** file to make sure Log4Net reflects changes in the configuration-
file during run-time.

## <i class="fal fa-forward-step" aria-hidden="true"></i> Step 7. Initialize Log4Net configuration

The code is different depending on the type of .NET run-time:

* [.NET Framework](#net-framework)
* [.NET](#net)

### .NET Framework

Example using a .NET Framework (>4.6.2) solution.

```csharp
var fileInfo = new System.IO.FileInfo("log4net.config");
XmlConfigurator.Configure(fileInfo);
```

### .NET

Worker service example.

```csharp
IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services =>
    {
        services.AddLogging(loggingBuilder =>
        {
            loggingBuilder.AddLog4Net();
        });
        services.AddHostedService<Worker>();
    })
    .Build();
```

## <i class="fal fa-forward-step" aria-hidden="true"></i> Step 8. Perform Logging

Perform the logging by adding code depending on the different Log levels, view examples below:  

### Log using the .NET Framework

```csharp
// Message is payload (Body)
log.Info("<Orders><Order><Id>123</Id></Order></Orders>");
log.Error("<Orders><Order><Id>123</Id></Order></Orders>", new Exception("Guru Meditation"));
```

*Here's an example of a simple .NET Framework-based Logging example.*

### Log using .NET

```csharp
public class Worker
 {
     private readonly ILogger<Worker> _logger;

     public Worker(ILogger<Worker> logger)
     {
         _logger = logger;
     }
     
     public void DoLogging()
     {
        _logger.LogInformation("<Orders><Order><Id>{OrderId}</Id></Order></Orders>", 123);
        _logger.LogError(ex, "<Orders><Order><Id>{OrderId}</Id></Order></Orders>", 123);
     }
}
```

*Here's an example of a simple .NET-based Logging example.*

### Error example (.NET)

### Override Configuration Values

<sup><span class="nodinite-new-badge">New 6.0.8</span></sup>
All configuration values can be overriden using the name as in the example below:  

```csharp
log4net.GlobalContext.Properties["ApplicationInterchangeId"] = "RUN-456";
log4net.GlobalContext.Properties["SequenceNo"] = 1;
log.Info("<Orders><Order><Id>123</Id></Order></Orders>");
```

*.NET Framework example.*

```csharp
log4net.GlobalContext.Properties["ApplicationInterchangeId"] = "RUN-456";
log4net.GlobalContext.Properties["SequenceNo"] = 2;
log4net.GlobalContext.Properties["LogText"] = "Finished Processing";
_logger.LogInformation(""); //NOTE: LogText is set on previous row
```

*.NET Framework example.*

### Add any Context

<sup><span class="nodinite-new-badge">New 6.0.8</span></sup>
You can add arbitrary Key/Values as to the `Context`. These will appear as **Context Properties** in the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [Log Event][]. Then, you can create [Search Fields][] and
use the [Message Context Key][] plugin to have <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> extract the values. Use this in <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [Log Views][] and allow your business users to gain access, and search for data.

```csharp
log4net.GlobalContext.Properties["Context"] = new Dictionary<string, string>
 {
     { "Key1", "Value1" },
     { "Key2", "Value2" }
 };
```

<aside aria-label="Info" class="nodinite-note nodinite-note--info">
<p class="nodinite-note__label" aria-hidden="true"><i class="far fa-fw fa-square-info"></i> Info</p>
<div class="nodinite-note__content"><p>The code above applies to .NET and .NET Framework.</p></div>
</aside>

### Example overriding all config values

```csharp
log4net.GlobalContext.Properties["LogText"] = "SAMPLE:LogText"; // NOTE: This value is reset after EACH log event
log4net.GlobalContext.Properties["LogApiServiceURI"] = "C:\\Code\\Nodinite.LogAgent.Log4NetAppender\\Nodinite.LogAgent.Log4NetAppender.TestClient\\Logs\\"; // Should "only" be set in config file
log4net.GlobalContext.Properties["SequenceNo"] = "0"; // This value should be set +1 before any Log operation

log4net.GlobalContext.Properties["ApplicationInterchangeId"] = "SAMPLE:ApplicationInterchangeId";
log4net.GlobalContext.Properties["OriginalMessageType"] = "SAMPLE:OriginalMessageType";
log4net.GlobalContext.Properties["LogAgentID"] = "42";
log4net.GlobalContext.Properties["EventDirection"] = "1";
log4net.GlobalContext.Properties["EventNumber"] = "1337";
log4net.GlobalContext.Properties["EndPointDirection"] = "1";
log4net.GlobalContext.Properties["EndPointName"] = "SAMPLE:EndPointName";
log4net.GlobalContext.Properties["EndPointUri"] = "SAMPLE:EndPointUri";
log4net.GlobalContext.Properties["ProcessingUser"] = "SAMPLE:ProcessingUser";
log4net.GlobalContext.Properties["ModuleType"] = "SAMPLE:ModuleType";
log4net.GlobalContext.Properties["ProcessName"] = "SAMPLE:ProcessName";
log4net.GlobalContext.Properties["ProcessingMachineName"] = "SAMPLE:ProcessingMachineName";
log4net.GlobalContext.Properties["ProcessingModuleType"] = "SAMPLE:ProcessingModuleType";
log4net.GlobalContext.Properties["ProcessingModuleName"] = "SAMPLE:ProcessingModuleName";
log4net.GlobalContext.Properties["ApplicationInterchangeId"] = "SAMPLE:ApplicationInterchangeId";
log4net.GlobalContext.Properties["LocalInterchangeId"] = "SAMPLE:LocalInterchangeId";
log4net.GlobalContext.Properties["ServiceInstanceActivityId"] = "SAMPLE:ServiceInstanceActivityId";

log4net.GlobalContext.Properties["Context"] = new Dictionary<string, string>
{
    { "Key1", "Value1" },
    { "Key2", "Value2" }
};
```

<i class="fal fa-book" aria-hidden="true"></i> Please review the [JSON Log Event][] user guide for details about each entry.  

---

## <i class="fal fa-hand-point-right" aria-hidden="true"></i> Next Steps

* <i class="fal fa-code" aria-hidden="true"></i> [JSON Log Event][] - The format the appender sends to the Log API  
* <i class="fal fa-tags" aria-hidden="true"></i> [Context Options][] - Add context properties to each event  
* <i class="fal fa-arrows-left-right" aria-hidden="true"></i> [Event Direction][EventDirections] - Set the direction of each event  
* <i class="fal fa-plug" aria-hidden="true"></i> [Endpoint Types][EndpointTypes] - Pick the [Endpoint][Endpoints] type for your events  

## <i class="fal fa-link-horizontal" aria-hidden="true"></i> Related Topics

* <i class="fal fa-cloud-arrow-down" aria-hidden="true"></i> [Log API][]  
* <i class="fal fa-file-lines" aria-hidden="true"></i> [What is a Log Event?][Log Event]  
* <i class="fal fa-sign-in-alt" aria-hidden="true"></i> [Endpoints][]  
* <i class="fal fa-puzzle-piece" aria-hidden="true"></i> [Log4Net Appender][]  

[1]:https://docs.nodinite.com/media/Documentation%20Pictures/LogAgent/Log4NetAppender/nuget_install_log4net.png

[Context Options]:https://docs.nodinite.com/core-services/log-event/features/context-options/
[Endpoint Directions]:https://docs.nodinite.com/core-services/log-event/features/endpoint-directions/
[Endpoints]:https://docs.nodinite.com/repository-model/endpoints/overview/
[EndpointTypes]:https://docs.nodinite.com/core-services/log-event/features/endpoint-types/
[EventDirections]:https://docs.nodinite.com/core-services/log-event/features/event-directions/
[File Monitoring Agent]:https://docs.nodinite.com/logging-and-monitoring/file-folder/overview/
[JSON Log Event]:https://docs.nodinite.com/core-services/log-event/features/json-log-event/
[Log API]:https://docs.nodinite.com/core-services/log-api/overview/
[Log Event]:https://docs.nodinite.com/core-services/log-event/overview/
[Log Views]:https://docs.nodinite.com/administration/log-management/log-views/features/what-is-a-log-view/
[Log4Net Appender]:https://docs.nodinite.com/logging-and-monitoring/log4net-appender/overview/
[Log4NetApache]:https://logging.apache.org/log4net/index.html
[Message Context Key]:https://docs.nodinite.com/administration/log-management/search-fields/features/search-field-expressions/message-context-key/
[Pickup Service]:https://docs.nodinite.com/logging-and-monitoring/pickup-logevents-service/overview/
[Search Fields]:https://docs.nodinite.com/administration/log-management/search-fields/features/what-is-a-search-field/
[Serilog]:https://docs.nodinite.com/logging-and-monitoring/serilog/overview/
[Servers step]:https://docs.nodinite.com/install-and-update/getting-started/version-7/install-nodinite-v7-steps/04-servers/#core-platform-services