C Sharper
C Sharper

Reputation: 8646

NLog variable changing its value in WebApi

I have two methods in my WebApi-

[HttpPost]
    public object Method1(JObject objJobject)
    {
        var log = LogManager.GetCurrentClassLogger(); 
        LogManager.Configuration.Variables["var1"] = "5";
        .
        .
    }

[HttpPost]
    public object Method2(JObject objJobject)
    {
        var log = LogManager.GetCurrentClassLogger(); 
        LogManager.Configuration.Variables["var1"] = "6";
        .
        .
    }

var1 is variable in nlog config :- ${var:var1}

My issue is that , when I call Method1 , It should consider var1 value as 5 for entire request execution. When I call Method2 , It should consider var1 value as 6 for entire request execution.

What happening in my case is :-

It is getting mixed for simultanous requests.

Eg. When Method1 is called , it considers var1=5 , but simultanously if user calls Method2 , It considers var1=6 var both Method1 and Method2.

What I want is , It should consider its respective value for entire execution.

Upvotes: 0

Views: 87

Answers (1)

Alexey.Petriashev
Alexey.Petriashev

Reputation: 1734

You should not use static global properties. In multithreaded envinronment you will get collisions.

Use EventProperties: https://github.com/nlog/NLog/wiki/EventProperties-Layout-Renderer

MDLC also can be used but it is some sort of global ambient context. EventProperties stores in LogEvent and can be used in layout or filter logic.

Upvotes: 1

Related Questions