log4net
Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud. Availability This functionality is generally available and ready for production usage. Serverless interfaces and procedures might differ from classic Elastic Stack deployments. The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats. Availability This functionality is generally available and ready for production usage. Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
Serverless
Generally available
Stack
Generally available
This Layout implementation formats a log4net event into a JSON representation that adheres to the Elastic Common Schema specification.
Add a reference to the Elastic.CommonSchema.Log4net package:
<PackageReference Include="Elastic.CommonSchema.Log4net" Version="8.6.0" />
Specify layout type in appender’s configuration:
<log4net>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="Elastic.CommonSchema.Log4net.EcsLayout, Elastic.CommonSchema.Log4net" />
</appender>
</log4net>
var hierarchy = (Hierarchy)LogManager.CreateRepository(Guid.NewGuid().ToString());
var appender = new ConsoleAppender { Layout = new EcsLayout() };
hierarchy.Root.AddAppender(appender);
hierarchy.Root.Level = Level.All;
hierarchy.Configured = true;
- Use the ECS layout.
The Layout = new EcsLayout() line then instructs log4net to use ECS layout. The sample above uses the console appender, but you are free to use any appender of your choice, perhaps consider using a filesystem target and Elastic Filebeat for durable and reliable ingestion.
Any valid ECS log template properties that is available under LogTemplateProperties.* e.g LogTemplateProperties.TraceId is supported and will directly set the appropriate ECS field.
Apart from mandatory fields, the output contains additional data:
log.origin.file.nameis taken fromLocationInformationlog.origin.file.lineis taken fromLocationInformationlog.origin.functionis taken fromLocationInformationevent.createdis taken from timestampevent.timezoneis equal to local timezonehost.hostnameis taken fromHostNamepropertyprocess.thread.idis taken fromThreadNameif it has numeric valueprocess.thread.nameis taken fromThreadNameif it doesn’t have numeric valueservice.nameis taken from entry or calling assemblyservice.versionis taken from entry or calling assemblyerror.messageis taken fromExceptionObjecterror.typeis taken fromExceptionObjecterror.stacktraceis taken fromExceptionObjectmetadatais taken from properties. It also contains message template and arguments in case a formatted message was logged
Sample log event output (formatted for readability):
{
"@timestamp": "2022-08-28T14:06:28.5121651+02:00",
"log.level": "INFO",
"message": "Hi! Welcome to example!",
"metadata": {
"global_property": "Example",
"message_template": "{0}! Welcome to example!"
"0": "Hi"
},
"ecs": {
"version": "8.3.1"
},
"event": {
"timezone": "Central European Time",
"created": "2022-08-28T14:06:28.5121651+02:00"
},
"host": {
"hostname": "HGU780D3"
},
"log": {
"logger": "Elastic.CommonSchema.Log4net.Example.Program",
"original": null,
"origin": {
"file": {
"name": "C:\\Development\\Elastic.CommonSchema.Log4net.Example\\Program.cs",
"line": 17
},
"function": "Main"
}
},
"process": {
"thread": {
"id": 1
}
},
"service": {
"name": "Elastic.CommonSchema.Log4net.Example",
"version": "1.0.0.0"
}
}