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

MappedDiagnosticsLogicalContext issues with ASP.NET Core 2.2 InProcess hosting #3387

Closed
SkinnySackboy opened this issue May 11, 2019 · 14 comments
Labels

Comments

@SkinnySackboy
Copy link

Hi,

Until recently, we were using NLog with our ASP.NET Core app without issue. We have configured log4jxmlevent using the MappedDiagnosticsLogicalContext. This has always worked for us.

Recently we switched to ASP.NET Core InProcess hosting and suddenly our log4jxmlevent layout renderer is no longer including data that we had initially set on the MappedDiagnosticsLogicalContext, because MappedDiagnosticsLogicalContext for some reason now gets cleared. The same goes for MappedDiagnosticsLogicalContext. More specifically, when we set them and log at startup it’s fine, but once the app is up and running and requests come in on different threads both these dictionaries get cleared.

The only one that doesn’t get cleared is GlobalDiagnosticsContext, however to my knowledge the log4jxmlevent does not attempt to GetNames() from here at any point.

Has anyone got any idea how to solve this problem please as I’ve been banging my head against this for a few days now and cannot find a satisfactory solution?

Thanks.

@welcome
Copy link

welcome bot commented May 11, 2019

Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!

@snakefoot
Copy link
Contributor

I guess the MappedDiagnosticsLogicalContext is difficult to use for global variables. Because ThreadPool-Threads will not inherit the AsyncLocal-context from the initial main-thread.

If you are using NLogViewer or Chainsaw-target then you can add <parameter>-items:

<target name="log4view" xsi:type="NLogViewer" address="tcp://127.0.0.1:878">
  <parameter layout="${gdc:item=MyGlobalVar}" name="GlobalParam"/>
</target>

@SkinnySackboy
Copy link
Author

We are currently doing this:

<target name="logFaces" xsi:type="Network" address="${var:logFacesUri}" layout="${var:logFacesLayout}" />

Where ${var:logFacesUri} and ${var:logFacesLayout} are set via code, something like this:

global::NLog.LogManager.Configuration.Variables["logFacesUri"] = logFacesUriBuilder.Uri.ToString(); global::NLog.LogManager.Configuration.Variables["logFacesLayout"] = $"${{log4jxmlevent:includeMdlc=true:includeCallSite=true:appInfo={softwareName}}}";

Is the nested parameter technique you described still supported for Log4JXMLEvent Layout Renderer?

Thanks for your quick response!

@snakefoot
Copy link
Contributor

snakefoot commented May 11, 2019

Is the nested parameter technique you described still supported for Log4JXMLEvent Layout Renderer?

NLogViewer or Chainsaw-target doesn't allow you to modify the layout. So you have to do it like this:

<target name="log4view" xsi:type="NLogViewer" address="${var:logFacesUri}" includeMdlc="true" includeCallSite="true">
  <parameter layout="${gdc:item=MyGlobalVar}" name="GlobalParam"/>
</target>

And stop using logFacesLayout.

Sadly enough then appInfo is not depending on NLog Layout, so it is "difficult" to modify at runtime. But you can do this:

NLog.LogManager.Configuration?.AllTargets.OfType<NLogViewer>().ToList().ForEach(t => t.AppInfo = softwareName);

@snakefoot
Copy link
Contributor

Have created the following pull-requests:

@SkinnySackboy
Copy link
Author

I don't know how to thank you enough - it worked and is much cleaner than what I was doing before. Thank you so much!

@snakefoot
Copy link
Contributor

snakefoot commented May 11, 2019

Happy you found a working solution. When the above PullRequests becomes part of NLog, then the config should become even easier/cleaner.

@SkinnySackboy
Copy link
Author

Thanks again for your help.

@snakefoot
Copy link
Contributor

@SkinnySackboy NLog 4.6.4 has been released: https://www.nuget.org/packages/NLog/4.6.4

So now you can do the following with a pure NetworkTarget:

<target name="logFaces" xsi:type="Network" address="${var:logFacesUri}">
   <layout xsi:type="Log4JXmlEventLayout" includeMdlc="true" includeCallSite="true">
       <parameter layout="${gdc:item=MyGlobalVar}" name="GlobalParam"/>
   </layout>
</target>

@SkinnySackboy
Copy link
Author

Thank you very much!

@SkinnySackboy
Copy link
Author

SkinnySackboy commented May 29, 2019

@snakefoot I now see the following error:

“Parameter includeCallSite not supported on Log4JXmlEventLayout”

@snakefoot
Copy link
Contributor

snakefoot commented May 29, 2019 via email

@snakefoot
Copy link
Contributor

Have now created #3442

@snakefoot
Copy link
Contributor

@SkinnySackboy Maybe this might work while waiting for PR to be merged/released:

<target name="logFaces" xsi:type="Network" address="${var:logFacesUri}">
   <layout xsi:type="Log4JXmlEventLayout" includeMdlc="true">
       <renderer includeCallSite="true" />
       <parameter layout="${gdc:item=MyGlobalVar}" name="GlobalParam"/>
   </layout>
</target>

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

No branches or pull requests

3 participants