Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load JSON based NLog config (e.g. declared in appsettings.json) + BeginScopeParser performance improvements #263

Merged
merged 3 commits into from
Apr 1, 2019

Conversation

snakefoot
Copy link
Contributor

@snakefoot snakefoot commented Feb 23, 2019

Load NLog config from appsettings.json using NLogLoggingConfiguration

Loading configuration like this:

var config = new ConfigurationBuilder()
  .SetBasePath(System.IO.Directory.GetCurrentDirectory())
  .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
  .Build();

LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog"));

Example JSON-config:

{
    "NLog": {
        "internalLogLevel": "Info",
        "internalLogFile": "c:/temp/console-example-internal.log",
        "throwConfigExceptions": "True",
        "extensions": {
            "NLog.Web.AspNetCore": {
                "assembly": "NLog.Web.AspNetCore"
            }
        },
        "targets": {
            "console": {
                "type": "Console",
                "layout": "${longdate}|${level:uppercase=true}|${logger}|${message}${exception:format=tostring}"
            },
            "file": {
                "type": "AsyncWrapper",
                "target": {
                    "wrappedFile": {
                        "type": "File",
                        "fileName": "c:/temp/console-example.log",
                        "layout": {
                            "type": "JsonLayout",
                            "Attributes": [
                                {
                                    "name": "timestamp",
                                    "layout": "${date:format=o}"
                                },
                                {
                                    "name": "level",
                                    "layout": "${level}"
                                },
                                {
                                    "name": "logger",
                                    "layout": "${logger}"
                                },
                                {
                                    "name": "message",
                                    "layout": "${message:raw=true}"
                                },
                                {
                                    "name": "properties",
                                    "encode":  false,
                                    "layout": {
                                        "type": "JsonLayout",
                                        "includeallproperties": "true"
                                    }
                                }
                            ]
                        }
                    }
                }
            },
        },
        "rules": [
            {
                "logger": "*",
                "minLevel": "Trace",
                "writeTo": "File,Console"
            }
        ]
    }
}

Also updated the BeginScopeParser to make use of NLog 4.6 optimizations.

Custom nuget-package can be found here:

https://ci.appveyor.com/project/nlog/nlog-framework-logging/builds/22600809/artifacts

@codecov
Copy link

codecov bot commented Feb 24, 2019

Codecov Report

Merging #263 into master will increase coverage by 0.16%.
The diff coverage is 69.34%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #263      +/-   ##
==========================================
+ Coverage   69.75%   69.92%   +0.16%     
==========================================
  Files          10       12       +2     
  Lines         873      941      +68     
  Branches      158      165       +7     
==========================================
+ Hits          609      658      +49     
- Misses        203      221      +18     
- Partials       61       62       +1
Impacted Files Coverage Δ
src/NLog.Extensions.Logging/Logging/NLogLogger.cs 81.54% <0%> (ø) ⬆️
...og.Extensions.Logging/Internal/StringExtensions.cs 100% <100%> (ø)
...g.Extensions.Logging/Logging/NLogLoggerProvider.cs 75.38% <100%> (+3.74%) ⬆️
...Extensions.Logging/Logging/NLogBeginScopeParser.cs 64.47% <59.72%> (-8.45%) ⬇️
...ensions.Logging/Config/NLogLoggingConfiguration.cs 79.66% <79.66%> (ø)
...ing/LayoutRenderers/ConfigSettingLayoutRenderer.cs 78.26% <0%> (+21.73%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2460b35...660b8e6. Read the comment docs.

@snakefoot snakefoot force-pushed the AppSettingsConfiguration branch from 2a370b2 to 857d859 Compare February 24, 2019 09:45
@snakefoot
Copy link
Contributor Author

snakefoot commented Feb 24, 2019

Performance Test using NLog 4.6-rc1 and NLog.Extension.Logging 1.5-rc1:

Really like how the ConcurrentQueue in NetCore21 is able to shine with heavy concurrency (See NLog PR Async NetCore21). Test machine only has 4 real cores (Intel i5), but still it handles 8 logger threads at full speed.

Each test is logging 500000 msgs, and for each msg it first calls BeginScope with two parameters.

Test with 1 Thread Msgs/sec CPU (ms) Alloc (MB)
NLog PR Net472 208753 24,2 6.331
NLog PR Async Net472 309035 16,5 6.332
NLog PR NetCore21 276207 18,3 6.942
NLog PR Async NetCore21 432242 13,4 6.942
NLog 4.5 Net472 171381 29,6 11.214
NLog 4.5 Async Net472 228333 22,6 11.221
NLog 4.5 NetCore21 223745 22,3 11.481
NLog 4.5 Async NetCore21 309091 16,5 11.481
Serilog 2.8 Net472 108431 45,5 19.607
Serilog 2.8 Async Net472 157687 62,4 19.669
Serilog 2.8 NetCore21 107938 45,4 14.152
Serilog 2.8 Async NetCore21 180890 54,2 12.169
Test with 2 Thread Msgs/sec CPU (ms) Alloc (MB)
NLog PR Net472 364760 27,3 6.331
NLog PR Async Net472 564409 23,6 6.332
NLog PR NetCore21 472452 20,3 6.942
NLog PR Async NetCore21 775853 18,5 6.942
NLog 4.5 Net472 303999 32,8 11.214
NLog 4.5 Async Net472 419450 28,5 11.221
NLog 4.5 NetCore21 383354 24,8 11.481
NLog 4.5 Async NetCore21 545154 22,5 11.482
Serilog 2.8 Net472 171943 55,8 21.745
Serilog 2.8 Async Net472 259365 54,4 21.808
Serilog 2.8 NetCore21 183252 50,2 14.152
Serilog 2.8 Async NetCore21 286060 48,2 12.174
Test with 4 Thread Msgs/sec CPU (ms) Alloc (MB)
NLog PR Net472 411157 46,6 6.332
NLog PR Async Net472 513216 28,9 6.333
NLog PR NetCore21 588766 29,8 6.942
NLog PR Async NetCore21 727490 17,3 6.942
NLog 4.5 Net472 400825 47,1 11.216
NLog 4.5 Async Net472 429616 33,4 11.228
NLog 4.5 NetCore21 591717 31,1 11.481
NLog 4.5 Async NetCore21 460658 24,9 11.482
Serilog 2.8 Net472 216956 84,0 21.748
Serilog 2.8 Async Net472 82714 223,5 22.735
Serilog 2.8 NetCore21 222516 72,8 14.152
Serilog 2.8 Async NetCore21 261713 65,7 12.853
Test with 8 Thread Msgs/sec CPU (ms) Alloc (MB)
NLog PR Net472 403770 48,3 6.342
NLog PR Async Net472 422297 38,4 6.347
NLog PR NetCore21 613755 29,4 6.942
NLog PR Async NetCore21 729017 17,1 6.942
NLog 4.5 Net472 394153 50,2 11.234
NLog 4.5 Async Net472 357785 44,1 11.242
NLog 4.5 NetCore21 583354 30,8 11.481
NLog 4.5 Async NetCore21 450397 25,8 11.482
Serilog 2.8 Net472 218886 86,4 23.920
Serilog 2.8 Async Net472 49706 379,4 25.515
Serilog 2.8 NetCore21 223629 72,5 14.152
Serilog 2.8 Async NetCore21 260969 69,5 13.154

@304NotModified 304NotModified added this to the 1.5 milestone Mar 9, 2019
304NotModified added a commit to NLog/NLog.github.io that referenced this pull request Mar 20, 2019
@snakefoot snakefoot force-pushed the AppSettingsConfiguration branch from 857d859 to 05213af Compare March 21, 2019 00:37
@304NotModified 304NotModified changed the title Load NLog config from appsettings.json using ExtensionLoggingConfiguration Load JSON based NLog config (e.g. declared in appsettings.json) Mar 24, 2019
@304NotModified 304NotModified self-requested a review March 24, 2019 22:58
@304NotModified 304NotModified self-assigned this Mar 24, 2019
@snakefoot snakefoot force-pushed the AppSettingsConfiguration branch from f12c796 to f40a87f Compare March 27, 2019 19:38
@304NotModified 304NotModified changed the title Load JSON based NLog config (e.g. declared in appsettings.json) Load JSON based NLog config (e.g. declared in appsettings.json) + BeginScopeParser performance Mar 28, 2019
@304NotModified 304NotModified changed the title Load JSON based NLog config (e.g. declared in appsettings.json) + BeginScopeParser performance Load JSON based NLog config (e.g. declared in appsettings.json) + BeginScopeParser performance improvements Mar 28, 2019
@snakefoot
Copy link
Contributor Author

snakefoot commented Mar 30, 2019 via email

@304NotModified
Copy link
Member

Because extensions is too generic and not bound to MEL?

@snakefoot
Copy link
Contributor Author

snakefoot commented Mar 30, 2019 via email

@304NotModified
Copy link
Member

Good for me!

@snakefoot snakefoot force-pushed the AppSettingsConfiguration branch from e400943 to 6c8c197 Compare April 1, 2019 05:30
@snakefoot
Copy link
Contributor Author

@304NotModified Resolved merge conflicts

@304NotModified 304NotModified force-pushed the AppSettingsConfiguration branch from 10079b9 to 660b8e6 Compare April 1, 2019 21:12
Copy link
Member

@304NotModified 304NotModified left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored a bit. Used only tooling for it (and I replace all), I'm pretty sure I didn't break a thing :)

If you like to review it, that OK with me, otherwise I will merge it now.

@snakefoot
Copy link
Contributor Author

If you like to review it, that OK with me, otherwise I will merge it now.

Everything looks good.

@304NotModified 304NotModified merged commit d7717f4 into NLog:master Apr 1, 2019
@304NotModified
Copy link
Member

🎉 merged!

@snakefoot
Copy link
Contributor Author

snakefoot commented Apr 2, 2019

Excellent. Then just need to ensure that default-wrapper is working, along with default-target-parameters and NLog variables.

@snakefoot snakefoot deleted the AppSettingsConfiguration branch April 2, 2019 19:25
@snakefoot
Copy link
Contributor Author

@304NotModified Added support for NLog variables: #282

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants