0
votes

I am using a programmatically configuration for NLog, where I added ApplicationInsights to log messages in Azure. But I don't know how to specify a layout for that message (e.g. to add the datetime in the message).

For example, for the file target I specified:

var fileTarget = new FileTarget();
fileTarget.Name = "file";
fileTarget.FileName = @"${basedir}/logs/${shortdate}.log";
fileTarget.Layout = @"${longdate} ${uppercase:${level}} ${message}";
config.AddTarget("file", fileTarget);

But for the Application Insights:

ConfigurationItemFactory.Default.Targets.RegisterDefinition(
                "ai", 
                typeof(ApplicationInsightsTarget)
);
ApplicationInsightsTarget aiTarget = new ApplicationInsightsTarget();
aiTarget.InstrumentationKey = "my_key";
aiTarget.Name = "ai";
config.AddTarget("ai", aiTarget);

How can I specify a layout for ai?

2
Looks like that can be implemented if AI target starts inheriting TargetWithLayout instead of Target and get message like this: string logMessage = this.Layout.Render(logEvent) (github.com/Microsoft/ApplicationInsights-dotnet-logging/blob/…). You can add an issue on the github or even implement it.Anastasia Black

2 Answers

3
votes

Starting from version 2.0.0 ApplicationInsights starts supporting layouts. You would configure it the same way as any other target. Release Notes

1
votes

As far as I recall, the AI target does not allow overriding the default layout.