azure-sdk-for-net
azure-sdk-for-net copied to clipboard
Using LogExporter to connect to AppInsights and set message and Custom Dimensions field
Library name and version
Azure.Monitor.OpenTelemetry.Exporter" Version="1.3.0"
Query/Question
Hey folks, we are looking to connect to AppInsights using .NET and OpenTelemetry. We have connected properly with Azure Monitor Log Exporter and can log to AppInsights. My query is in AppInsights we have 2 columns which are Message and CustomDimensions. I am using logger.log method to log data. This is the function used
void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
1 : On passing String as the 3rd param in Tstate state, it is setting the text as Message field in AppInsights. 2 : On passing Key-Value pair as 3rd param in Tstate state, it is setting as Custom Dimensions field in AppInsights.
Adding screenshot for both scenarios.
My question is how to set both fields, message and customDimensions?
Environment
No response
@srivadiMicrosoft Could you please provide us Minimal, Reproducible Example which explains this issue? Once we have the repro will check to identify why one of the column was not set.
@rajkumar-rangaraj The issue is based on type of Input I am able to set either "Message" or "CustomDimensions" field. I want a way to set both. Please see screenshot attachted in original question for output of above code
Looks like you are incorrectly using log string interpolation. Again interpolation is not recommended. Please follow the recommendation documented here that should solve the issue - https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/docs/logs#structured-logging
using Microsoft.Extensions.Logging; using OpenTelemetry.Logs; using Azure.Monitor.OpenTelemetry.Exporter; using System.Text.Json;
ILoggerFactory loggerFactory = LoggerFactory.Create(builder => { builder.AddOpenTelemetry(options => { options .AddAzureMonitorLogExporter(options => { options.ConnectionString = ; }) .AddConsoleExporter(); }); });
ILogger logger = loggerFactory.CreateLogger<Program>();
var person = new Dictionary<string, object?>() { ["Name"] = "Test", ["Age"] = 26, ["Country"] = "India" };
// This only sets Message field
logger.Log( LogLevel.Information, 0, "BodyBody", null, null);
// This only sets CustomDimensions field
logger.Log( LogLevel.Information, 0, person, null, null);
// Dispose logger factory before the application ends. // This will flush the remaining logs and shutdown the logging pipeline. loggerFactory.Dispose();
Not sure this answers my question. As you can see that we have 2 different fields in AppInsights, namely message and Custom Dimensions. I want to set both the fields.
In the above code using the 2 ways, I able to set either of the fields and not both. What is the way to set both fields? I have highlighted in above code on what does what
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @cijothomas @rajkumar-rangaraj @reyang @TimothyMothra @vishweshbankwar.
@srivadiMicrosoft My earlier recommendation was to follow the samples from https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/docs/logs#structured-logging. The action plan has not changed yet; please follow the samples in that article. Those samples update both the Message and custom dimensions.