Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Nlog: set user/session id possible? #195

Closed
fabsenet opened this issue May 30, 2018 · 3 comments
Closed

Nlog: set user/session id possible? #195

fabsenet opened this issue May 30, 2018 · 3 comments

Comments

@fabsenet
Copy link

fabsenet commented May 30, 2018

I have nlog logging to appinsights. it is configured in the app.config without any keys.

in the program startup I call:

    const string ik = "123123123...";
    TelemetryConfiguration.Active.InstrumentationKey = ik;
    var tc = new TelemetryClient
    {
        InstrumentationKey = ik
    };

    tc.Context.User.Id = settings.InstallationId.ToString();
    tc.Context.Session.Id = Guid.NewGuid().ToString();

I use the TelemetryClient to log pageviews. I see pageviews and traces from nlog in my appinsights. What I want now is to provide the user_id and the session_id in each trace but I do not know how to set this up? the pageviews have a user/session id already.

thanks

@snakefoot
Copy link
Contributor

You can setup a custom layout renderer to return the wanted values:

https://github.com/nlog/nlog/wiki/How-to-write-a-custom-layout-renderer

And add contextproperty to output them (See example):

#183

@Dmitry-Matveev
Copy link
Member

Another way you can try is to implement your own Telemetry Initializer and assign required properties on the outgoing telemetry. Chances are that TelemetryClient used in NLog adapter is actually created with TelemetryConfiguration.Active as a configuration, so any initializer you add into applicationinsights.config should be executed for those traces. This is more-less generic AI approach on adding custom properties to the outgoing item.

snakefoot's suggestion, however, seems to be right approach from NLog view on this - you simply expose/propagate the properties you care about.

@fabsenet
Copy link
Author

It works.

solution was to add this to the app.config:

<target type="ApplicationInsightsTarget" name="aiTarget">
        <contextProperty name="user_id" layout="${gdc:item=user_id}" />
        <contextProperty name="session_id" layout="${gdc:item=session_id}" />
</target>

and somewhere in the app startup:

GlobalDiagnosticsContext.Set("user_id", "some user id");
GlobalDiagnosticsContext.Set("session_id", "some session id");

thanks!

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

No branches or pull requests

3 participants