-
This plugin integrates log4net with Akka.NET, and enhances your Akka.NET logging capabilities by integrating with log4net. For detailed usage and configuration examples, see the sections below or explore our documentation.
-
Currently targetting Log4Net 2.0.17
With log4net, you can enrich your logs with both custom and automatic context information, such as file names, line numbers, and method names. This makes your logs more informative and easier to trace.
Use Log4NetLoggingAdapterExtensions to get an ILoggerAdapter that supports enriched logging:
Simply calling ForContext without arguments adds useful context like file name, line number, and method name to your logs:
var logger = Context.GetLogger().ForContext();
logger.Info("This log includes automatic context information.");Add custom properties to your logs as follows:
var logger = Context.GetLogger()
.ForContext("CustomProperty1", "CustomValue1")
.ForContext("CustomProperty2", "CustomValue2");
logger.Info("This log has custom properties.");Or, use a dictionary for multiple properties (an enumerable of key-value pairs):
var properties = new Dictionary<string, object?>
{
["UserId"] = "user123",
["Operation"] = "UpdateProfile"
};
var logger = Context.GetLogger().ForContext(properties);
logger.Info("User profile updated.");This will log a message with custom properties UserId and Operation, along with the source file name, line number, and method name.
To include custom properties in your logs, configure log4net like this:
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] [%file(%line)] [%property{LogSource} - %property{ActorPath}] %-5level %logger - UserId=%property{UserId}, Operation=%property{Operation} %class.%method - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>This setup ensures your logs include all the specified details, making them more informative and easier to navigate.
Log4net can be registered as the Akka.NET logger through the Akka.Hosting integration. This approach provides a clean, modern way to wire up logging in hosted applications using dependency injection.
Use the ConfigureLoggers method on your Akka configuration builder:
using Akka.Hosting;
using Akka.Logger.log4net;
// builder is a HostApplicationBuilder / WebApplicationBuilder
builder.Services.AddAkka("MySystem", configurationBuilder =>
{
configurationBuilder.ConfigureLoggers(loggerConfigBuilder =>
{
loggerConfigBuilder.ClearLoggers(); // remove Akka's default console logger
loggerConfigBuilder.AddLogger<Log4NetLogger>(); // route Akka events into log4net
});
});Log4net appenders and the repository are configured as usual via log4net.config (using XmlConfigurator) or programmatically via the log4net API.
For a complete runnable example, see src/Examples/Akka.Logger.log4net.HostingDemo.
The log4net integration enhances Akka.NET's logging by allowing for detailed contextual information in logs. This not only improves diagnostics but also aids in understanding the execution flow and context of log messages.
- Akka.NET Team