{"action":"created","issue":{"url":"https://api.github.com/repos/NLog/NLog/issues/869","labels_url":"https://api.github.com/repos/NLog/NLog/issues/869/labels{/name}","comments_url":"https://api.github.com/repos/NLog/NLog/issues/869/comments","events_url":"https://api.github.com/repos/NLog/NLog/issues/869/events","html_url":"https://github.com/NLog/NLog/issues/869","id":102357441,"number":869,"title":"Instance-context instead of thread-local-context?","user":{"login":"SystemKeeper","id":1580193,"avatar_url":"https://avatars.githubusercontent.com/u/1580193?v=3","gravatar_id":"","url":"https://api.github.com/users/SystemKeeper","html_url":"https://github.com/SystemKeeper","followers_url":"https://api.github.com/users/SystemKeeper/followers","following_url":"https://api.github.com/users/SystemKeeper/following{/other_user}","gists_url":"https://api.github.com/users/SystemKeeper/gists{/gist_id}","starred_url":"https://api.github.com/users/SystemKeeper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SystemKeeper/subscriptions","organizations_url":"https://api.github.com/users/SystemKeeper/orgs","repos_url":"https://api.github.com/users/SystemKeeper/repos","events_url":"https://api.github.com/users/SystemKeeper/events{/privacy}","received_events_url":"https://api.github.com/users/SystemKeeper/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-08-21T11:32:44Z","updated_at":"2015-08-21T13:05:04Z","closed_at":null,"body":"Hello,\r\n\r\nI'm currently using NLog 4.0.1 and want to add additional informations when logging from a specific class. Consider this class for example:\r\n```vbnet\r\nPublic Class Device\r\n  Private Shared logger As NLog.Logger = NLog.LogManager.GetCurrentClassLogger()\r\n  Private DeviceId as Integer\r\n\r\n  Public Sub New(DeviceId as Integer)\r\n    Me.DeviceId = DeviceId\r\n  End Sub\r\n\r\n  Public Sub TestLog()\r\n    logger.Debug(\"TestLog\")\r\n  End Sub\r\nEnd Class\r\n```\r\nI'm creating multiple instances of this class with a different DeviceId and store the reference in a List:\r\n```vbnet\r\nDim DeviceList As New List(Of Device)\r\nFor i = 0 To 20\r\n  DeviceList.Add(new Device(i))\r\nNext\r\n```\r\nNow all calls to the device-classes are done using the Task-Library:\r\n```vbnet\r\nFor Each Dev in DeviceList\r\n  Task.Factory.StartNew(Sub()\r\n      Dev.TestLog()\r\n    End Sub)\r\nNext\r\n```\r\nThis works fine so far, but I'm unable to use something like MDC as the Task-Lib is using a ThreadPool and adding the DeviceId to the MDC will result in wrong outputs if the TestLog-Call is done by a different thread.\r\nTherefore I was wondering if it would be possible to add something like a instance-context to the logger - in this case a logger must not be static/shared:\r\n```vbnet\r\nPublic Class Device\r\n  Private logger As NLog.Logger = NLog.LogManager.GetCurrentInstanceLogger()\r\n  Private DeviceId as Integer\r\n\r\n  Public Sub New(DeviceId as Integer)\r\n    Me.DeviceId = DeviceId\r\n    logger.Context.set(\"DeviceId\", DeviceId)\r\n  End Sub\r\n  (...)\r\nEnd Class\r\n```\r\nIs something like this possible or is there another way to achieve this? Currently I'm using a string-variable which is set in the constructor of Device and include it in all log-calls. This works, but is not really nice.\r\n\r\nThanks in advance!"},"comment":{"url":"https://api.github.com/repos/NLog/NLog/issues/comments/133421058","html_url":"https://github.com/NLog/NLog/issues/869#issuecomment-133421058","issue_url":"https://api.github.com/repos/NLog/NLog/issues/869","id":133421058,"user":{"login":"304NotModified","id":5808377,"avatar_url":"https://avatars.githubusercontent.com/u/5808377?v=3","gravatar_id":"","url":"https://api.github.com/users/304NotModified","html_url":"https://github.com/304NotModified","followers_url":"https://api.github.com/users/304NotModified/followers","following_url":"https://api.github.com/users/304NotModified/following{/other_user}","gists_url":"https://api.github.com/users/304NotModified/gists{/gist_id}","starred_url":"https://api.github.com/users/304NotModified/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/304NotModified/subscriptions","organizations_url":"https://api.github.com/users/304NotModified/orgs","repos_url":"https://api.github.com/users/304NotModified/repos","events_url":"https://api.github.com/users/304NotModified/events{/privacy}","received_events_url":"https://api.github.com/users/304NotModified/received_events","type":"User","site_admin":false},"created_at":"2015-08-21T13:05:04Z","updated_at":"2015-08-21T13:05:04Z","body":"Too bad that isn't possible (yet). What you can do:\r\n\r\nuse the GDC and prefix the key with the loggername. You can write some nice C# extensions to keep the code clean. "}}