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

Repeating db records multiple times #3770

Closed
vikassaini80 opened this issue Jan 22, 2020 · 9 comments
Closed

Repeating db records multiple times #3770

vikassaini80 opened this issue Jan 22, 2020 · 9 comments

Comments

@vikassaini80
Copy link

Hello All

I am facing a issue with log db while i am inserting records in db its inserting multiple times while in file the same record is in single time, while i am checking the trace file its showing the exact no that i am able to see in my db but reason i dint know, Can someone please look into that and do let me know what causing the issue.

I am attaching trace file and nlog.config file please review it and do let me know what causing the issue.

NLog.zip

@welcome
Copy link

welcome bot commented Jan 22, 2020

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

@vikassaini80
Copy link
Author

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

Please send me template

@304NotModified
Copy link
Member

304NotModified commented Jan 22, 2020

Hi! Thanks for reporting this feature/bug/question!

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

QUESTIONS are preferred on StackOverflow. You could expect a faster response there (but do include all the info below). Use the tag "nlog" https://stackoverflow.com/questions/ask

For .NET Core users, please check the platform support: https://github.com/NLog/NLog/wiki/platform-support

NLog version: (e.g. 4.4.13)

Platform: .Net 3.5 / .Net 4.0 / .Net 4.5 / Mono 4 / Silverlight 4 / Silverlight 5 / Xamarin Android / Xamarin iOs / .NET Core 1 / .NET Core 2

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

<nlog>
  <targets>
  </targets>
  <rules>
  </rules>
</nlog>
  • 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?
  • Can you help us by writing an unit test?

@snakefoot
Copy link
Contributor

snakefoot commented Jan 22, 2020

@vikassaini80 I can see from you Nlog.config that you are very happy about ${var:}. NLog Global Config Variables should NOT be used for logging local context information:

      <parameter name="@message" layout="${var:message}" />
      <parameter name="@stacktrace" layout="${var:stacktrace}" />

This goes especially bad, when using <targets async="true"> as you have no control of when the NLog Global Variables are assigned, and when they are read, even if you own logic is single-threaded.

Instead you should use the standard means for capturing context:

https://github.com/NLog/NLog/wiki/Context

The most optimal (and most simple) is capturing using the LogEventInfo.Properties that can rendered using ${event-properties}:

var logEvent = LogEventInfo() { Level = LogLevel.Info, Message = "Hello" };
logEvent.Properties["stacktrace"] = "HelloWorld.cs: Line 42";
logger.Log(logEvent);

The more advanced solution is using MappedDiagnosticsLogicalContext - ${mdlc}:

using (NLog.MappedDiagnosticsLogicalContext.SetScoped("clientCorrelationID", Guid.NewGuid()))
{
   logger.Info("myLogEvent");
}

@snakefoot snakefoot changed the title Reapting db records multiple times Repeating db records multiple times Jan 22, 2020
@vikassaini80
Copy link
Author

  <parameter name="@message" layout="${var:message}" />
      <parameter name="@stacktrace" layout="${var:stacktrace}" />

Thanks For your reply, we are using {var:} to assigned the value and same we are sending to the db query

can you please little elaborate how we can insert record in db using LogEventInfo() that will be really helpful.

@vikassaini80
Copy link
Author

  • 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?
  • Can you help us by writing an unit test?

I have attached my nlog.config and trace file with this issue

What is the current result? Some time we saw a single record is multiple times in db only while the same record in file is only single time which is expected.
What is the expected result? A unique record must be exist only once in db as we saw in nlog file
Did you checked the Internal log? Yes, I attached all with this issue
Please post full exception details (message, stacktrace, inner exceptions): There is no such exception only we saw multiple records for a single unique transaction and problem is that its too random its happening during load testing or server has thousands transaction per second, So even we want to replicate in our local environment we are unable to do that because its very random not for all the transactions.
Are there any workarounds? yes/no : we tried to trace using nlog internal tracing and tried to put async=false and tried to remove that as well
Is there a version in which it did work? I am not sure
Can you help us by writing an unit test? NO

@snakefoot
Copy link
Contributor

@vikassaini80 can you please little elaborate how we can insert record in db using LogEventInfo() that will be really helpful.

Have you even tried to read the Wiki-page about using Context that I gave to you?

Example code:

var logEvent = LogEventInfo() { Level = LogLevel.Info, Message = "Hello" };
logEvent.Properties["stacktrace"] = "HelloWorld.cs: Line 42";
logger.Log(logEvent);

Example config:

      <parameter name="@message" layout="${message}" />
      <parameter name="@stacktrace" layout="${event-properties:stacktrace}" />

@304NotModified
Copy link
Member

Closing this due to inactivity. Please let us know if this still an issue and please provide the requested info -
thanks!

@304NotModified
Copy link
Member

304NotModified commented Feb 9, 2020

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

No branches or pull requests

3 participants