ecs-dotnet icon indicating copy to clipboard operation
ecs-dotnet copied to clipboard

Add log4net support

Open andreycha opened this issue 3 years ago • 1 comments

This PR adds support for log4net via ECS layout.

Changes:

  • Added log4net ECS layout
  • Added unit tests
  • Updated site documentation

Please let me know if I missed anything.

Some open questions:

  • [ ] Naming of the library: Elastic.CommonSchema.Log4net or Elastic.CommonSchema.log4net? Logging library is officially spelled as log4net.
  • [ ] Do we want to provide the possibility to enrich logging event before logging? Similar to NLog layout implementations. Though this is only useful in case of programmatical configuration which I believe is a small minority of the use cases.
  • [ ] Currently layout populates service.version field, but this might be a wrong value. E.g., in case of IIS hosted applications GetEntryAssembly() return null so version of calling the calling assembly is used which might be different from the application version. Keep or drop?
  • [ ] Is example application needed?

andreycha avatar Aug 28 '22 10:08 andreycha

:green_heart: Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2022-09-06T07:06:04.528+0000

  • Duration: 27 min 18 sec

Test stats :test_tube:

Test Results
Failed 0
Passed 152
Skipped 2
Total 154

:robot: GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

apmmachine avatar Aug 28 '22 10:08 apmmachine

Jenkins run tests please

Mpdreamz avatar Sep 05 '22 17:09 Mpdreamz

  • Naming of the library: Elastic.CommonSchema.Log4net or Elastic.CommonSchema.log4net? Logging library is officially spelled as log4net.

I have no strong opinion but it seems common for the suffix to be Log4Net on nuget.org. I think my vote would go to adhere to regular namespacing rules.

  • Do we want to provide the possibility to enrich logging event before logging? Similar to NLog layout implementations. Though this is only useful in case of programmatical configuration which I believe is a small minority of the use cases.

I'm fine leaving this out until we see strong evidence this is needed.

  • Currently layout populates service.version field, but a wrong value might end up there. E.g., in case of IIS hosted applications GetEntryAssembly() returns null so version of the calling assembly is used which might be different from the application version. Keep or drop?

Drop, best to allign with what the Elastic Apm Agent does here:

https://github.com/elastic/apm-agent-dotnet/blob/305be0a9017ce9be50b4b269caa7404d14974cbb/src/Elastic.Apm/Config/AbstractConfigurationReader.cs#L853-L860

  • Is example application needed?

No, the readme covers everything already IMO.

Mpdreamz avatar Sep 05 '22 17:09 Mpdreamz

Drop, best to allign with what the Elastic Apm Agent does here: https://github.com/elastic/apm-agent-dotnet/blob/305be0a9017ce9be50b4b269caa7404d14974cbb/src/Elastic.Apm/Config/AbstractConfigurationReader.cs#L853-L860

Dropped for now. Imho best would be to implement later this approach for all three packages.

andreycha avatar Sep 05 '22 18:09 andreycha

21:13:55  [xUnit.net 00:00:01.00]     Elastic.CommonSchema.Log4net.Tests.MessageTests.ToEcs_AnyEvent_PopulatesHostField [FAIL]
21:13:55    Failed Elastic.CommonSchema.Log4net.Tests.MessageTests.ToEcs_AnyEvent_PopulatesHostField [36 ms]
21:13:55    Error Message:
21:13:55     Expected info.Host.Hostname to be 
21:13:55  "APM-CI-IMMUTABL" with a length of 15, but 
21:13:55  "apm-ci-immutable-windows-2019-1662404522828227605" has a length of 49, differs near "apm" (index 0).

It seems log4net lowercases hostname?

Mpdreamz avatar Sep 05 '22 19:09 Mpdreamz

It seems log4net lowercases hostname?

Not really. It determines hostname using several sources in certain order. Whereas in the test I just compared the value against Environment.MachineName which returns hostname in upper case and truncated to 15 characters (I wasn't aware of such behavior). Fixed the test.

andreycha avatar Sep 05 '22 20:09 andreycha

Merged, thanks for donating this implementation @andreycha :+1:

Mpdreamz avatar Sep 06 '22 11:09 Mpdreamz