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

NLogTarget: How to exclude a log event property from telemetry? #2159

Closed
muratkuru opened this issue Jan 25, 2021 · 2 comments
Closed

NLogTarget: How to exclude a log event property from telemetry? #2159

muratkuru opened this issue Jan 25, 2021 · 2 comments

Comments

@muratkuru
Copy link

  • List of NuGet packages and version that you are using: Microsoft.ApplicationInsights.NLogTarget
  • Runtime version (e.g. net461, net48, netcoreapp2.1, netcoreapp3.1, etc. You can find this information from the *.csproj file): netcoreapp3.1
  • Hosting environment (e.g. Azure Web App, App Service on Linux, Windows, Ubuntu, etc.): Windows

What are you trying to achieve?

There is logging process like below

        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IActionResult Get()
        {
            string request = string.Empty;
            string response = string.Empty;
            TimeSpan elapsedTime = TimeSpan.Zero;

            _logger.Log(LogLevel.Information,
                default,
                new ServiceLogEvent(request, response, elapsedTime),
                default,
                (l, e) => string.Empty);

            return NoContent();
        }

and contextproperties in nlog.config like below

      <contextproperty name="request" layout="${masked:${event-properties:Request}}"/>
      <contextproperty name="response" layout="${masked:${event-properties:Response}}"/>

These context properties (request, response) logged to Application Insights as masked as well as expected. But also there are log event properties in the log object unexpectedly.

{
    "request": "{ \"phone\": \"******\" }",
    "response": "{ \"result\": \"Some *** *** data.\" }",
    "Request": "{ \"phone\": \"1111111111\" }", 
    "Response": "{ \"result\": \"Some other sensitive data.\" }",
}

Is this a bad approach or is there any workaround?

What have you tried so far?

I am using a custom ApplicationInsightsTarget (forked from this repo) who has an ExcludeLogEventProperty property for now.

    <target 
      xsi:type="ApplicationInsightsTarget" 
      name="ApplicationInsights">
      <contextproperty name="request" layout="${masked:${event-properties:Request}}"/>
      <contextproperty name="response" layout="${masked:${event-properties:Response}}"/>
      <excludelogeventproperty name="Request" />
      <excludelogeventproperty name="Response" />
    </target>

I can open a PR for this method if it will be useful.

@github-actions
Copy link

This issue is stale because it has been open 300 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@snakefoot
Copy link
Contributor

Will be resolved with #2103 and updating to NLog 5.0 (See also NLog/NLog#4642)

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

2 participants