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

How to get the Request Properties like RequestPath and RequestDetail in a class file, update it and add them back to the logs #5521

Closed
rchilukoti opened this issue May 16, 2024 · 7 comments
Labels

Comments

@rchilukoti
Copy link

rchilukoti commented May 16, 2024

Hi! Thanks for reporting this bug!

Please keep / fill in the relevant info from this template so that we can help you as best as possible.

NLog version: (e.g. 5.2.7)

Platform: . .NET6

Current NLog config (xml or C#, if relevant): xml

My config details included have been blanked out so adding it again.

<nlog>
  <targets>
   <target>
    <layout xsi:type="JsonLayout">
        ........
      <attribute name="EventProperties" encode="false">
        <layout type="JsonLayout" includeMdlc="true" includeAllPrpoerties="true" maxRecursionLimit="10">
           <attribute name="RequestIp" layout="${asp-request-ip}" />
           <attribute name="RequestMethod" Layout="${asp-request-method}" />
           <attribute name="ResponseStatusCode" layout="${asp-response-statuscode" />
        </layout>
     </attribute>
   </target>
  </targets>
  <rules>
  </rules>
</nlog>

Hi,
I am new to Nlog and still learning, I have a question that I am struggling with from past couple of days.
My nlog.config looks something like this.

<attribute name= "EventProperties" encode="false">
   <layout type="JsonLayout" includemdlc="true" includeAllProperties="true" maxRecursionlimit="10">
      <attribute name= "RequestIp" layout="${aspnet-request-ip}">
      <attribute name= "RequestMethod" layout="${aspnet-request-method}">
      <attribute name= "ResponseStatuscode" layout="${aspnet-response-statuscode}">
   </layout>
</attributes>

Question - I would like to read the Request details like RequestPath and RequestDetail in my .Net 6.0 class library class file, change it and update the lvalues back to the ogs after making an api call.
The log level is info.
All these fields show in the logs which would like to read and update.
can you please let me know how I can do this in c# and if any changes are needed to the config file?

Thanks

  • What is the current result?
  • What is the expected result?
  • Did you checked the Internal log?
  • Please post full exception details (message, stacktrace, inner exceptions)
  • Are there any workarounds? yes/no
  • Is there a version in which it did work? 5.2.7
  • Can you help us by writing an unit test?
Copy link

welcome bot commented May 16, 2024

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

snakefoot commented May 17, 2024

When troubleshooting NLog then it is a good idea to enable throwConfigExceptions="true" and maybe check the NLog InternalLogger for warnings and errors.

If the existing layout-renderers are not doing what you want, then you can create your own custom layout-renderer:

NLog.LogManager.Setup().RegisterNLogWeb().SetupExtensions(ext =>
  ext.RegisterAspNetLayoutRenderer("test-web", (logEventInfo, httpContext, loggingConfiguration) =>
  {
     return httpContext.Session["SessionItem"];
  })
);

@rchilukoti
Copy link
Author

rchilukoti commented May 17, 2024

Thanks, snakefoot.
I tried with a dynamic variable in "nlog.config" .
Assigned the value to it in "nlog.config" after obtaining the value in my class library class file using for the "context,Request" and updating it.
After unit testing looks like it is working as I can see the expected results,
Are there any drawbacks of using a dynamic variable for an application that is deployed on Azure AKS(running multiple instances of the application in pods)?
Thanks

@snakefoot
Copy link
Contributor

snakefoot commented May 18, 2024

If "dynamic variable" then you mean NLog Configuration Variables (Ex. LogManager.Configuration.Variables["user"] = "admin"), then they are global variables and you will have bad behavior when having multiple concurrent requests.

Maybe consider using the NLog ScopeContext, where the class-library can provide its own custom context, and then have fallback when none provided.

  • ${scopeProperty:RequestPath:whenEmpty=${aspnet-request-url}}

Then the class library can do this:

using (NLog.ScopeContext.PushProperty("RequestPath", "My Request")) 
{
    Logger.Info("Hello World"); // LogEvent can use ${scopeProperty:RequestPath} in target output
}

Alternative you can also provide additional details using LogEvent properties for example using structured logging:

Logger.Info("Hello from {RequestDetail}", "My Detail");

@rchilukoti
Copy link
Author

rchilukoti commented May 19, 2024

@snakefoot
Thank you very much for the reply.
I tried the scopeContext as suggested by you above but somehow it is not working for me.
Are there any config settings missing when working with scopeContext?
Only configuration looks working for me and looks like it is not a good option when running multiple instances.

@snakefoot
Copy link
Contributor

Without any hints of what you are trying, and the results you see, then it is hard to help.

When troubleshooting NLog then it is a good idea to enable throwConfigExceptions="true" and maybe check the NLog InternalLogger for warnings and errors.

@snakefoot
Copy link
Contributor

Closing due to inactivity.

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

2 participants