I recently worked on a .Net application that sends its logs to files on the local file system using the log4Net
Framework (see the documentation here).
With the objective of improving and centralising logs, we considered sending the logs to an Azure Application Insight.
Of course, we can use the App Insight SDK, but to avoid multiplying the code, we decided to extend Log4nNet by using a Log4Net appender dedicated to App Insight.
In this post walks you through the steps to configure Log4net with the Application Insights appender and view your logs in the Azure portal.
Prerequisites
- Azure Application Insights: Set up a resource in the Azure portal.(see the documentation here)
- The Instrumentation Key: Obtain the instrumentation key from your Application Insights resource. You’ll use this to send logs to Azure.
Install the Required NuGet Package
To enable logging from log4net to Application Insights, install the Microsoft.ApplicationInsights.Log4NetAppender
package using the command or via Visual Studio Package Manager:
dotnet add package Microsoft.ApplicationInsights.Log4NetAppender
Update log4net.config or web.config
Modify your log4net.config
(or web.config
) file to include the Application Insights appender. Below is a sample configuration:
<log4net>
<appender name="ApplicationInsightsAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
<param name="InstrumentationKey" value="**<your-instrumentation-key>**" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="ApplicationInsightsAppender" />
</root>
</log4net>
Replace your-instrumentation-key
with the key from your Azure Application Insights resource.
You can also configure Telemetry directly in he code. Bellow a sample of telemetry configuration:
using Microsoft.ApplicationInsights.Extensibility;
TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
configuration.InstrumentationKey = "your-instrumentation";
Initialize log4net in Your Application
Ensure log4net is initialized when your application starts. Here’s an example for an ASP.NET application:
using log4net;
namespace YourNamespace
{
public class Program
{
private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
public static void Main(string[] args)
{
Log.Info("Application is starting...");
// Your code here
}
}
}
This ensures log4net reads from the log4net.config
file and starts logging immediately.
Start your application and trigger log events.
View Logs in Azure Application Insights
Once your logs are being sent to Application Insights, you can explore and analyze them in the Azure portal. Here's how:
- Go to Your Application Insights Resource
- Open the left menu Transaction search
- Click on the button "See all data in the last 24 hours"
The logs appear.
You can filter by date and event type of logs (trace, request, exception, ....).
Bellow a sample of logs in App Insight, that I filter to last 30 days logs and choose to display only logs of type Exceptions:
Troubleshooting Tips
- Missing Logs: Double-check the instrumentation key and verify your application has network access to Azure.
- Log Level Issues: Confirm the log level in your log4net.config matches your expectations (e.g., INFO, DEBUG, ERROR).
-
Debug Locally: Temporarily add a
ConsoleAppender
in log4net.config to verify logs are being generated locally.
Conclusion
Integrating log4net with Azure Application Insights is a powerful way to monitor your application's behavior. With these steps, you’ll have logging configured and the ability to explore logs in the Azure portal for faster debugging and optimization. You can even set up alerts to proactively monitor critical issues.
Top comments (0)