I have a simple Azure Function and I'd like to be able to monitor log output in both the streaming log window and in Application Insights.
So far I am able to see NLog output in Application Insights but not in the streaming window. Microsoft ILogger output appears in both.
Here's what I've done so far:
- Created a basic Azure test function
- From the Azure portal, I enabled Application Insights
- Installed NLog and Microsoft.ApplicationInsights.NLogTarget nuget dependencies for the function.
- Added code to the function to programmatically create an Application Insights nlog target.
- After reading these questions... Azure Log Streaming with NLog and How to integrate NLog to write log to Azure Streaming log I have also programmatically added an NLog TraceTarget since they suggest that the streaming log mechanism only displayed the Trace ouput stream (not to be confused with the log-level called 'Trace' !)
Finally, I have modified host.json to turn the default logging level up to "Trace". This is required in order for the streaming window to show Trace-level output for the Microsoft ILogger and I had thought it might be the reason none of the Nlog output was showing...
{ "version": "2.0", "logging": { "logLevel": { "default": "Trace" } } }
So far, the results are that:
- All Microsoft logger output is shown in both the streaming window and Application Insights log.
- The Nlog output is shown in Application Insights but does NOT appear in the streaming window.
Here is the final function code...
public static class Function1
{
[FunctionName("LogTest")]
public static void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
{
// Set up NLOG targets and logger
var config = new LoggingConfiguration();
//send logging to application insights
config.LoggingRules.Add(
new LoggingRule("*", LogLevel.Trace,
new ApplicationInsightsTarget()));
//also try to log to Trace output
//'RawWrite' is used to try and force output at all log-levels
config.LoggingRules.Add(
new LoggingRule("*", LogLevel.Trace,
new TraceTarget {RawWrite = true}));
LogManager.Configuration = config;
var nlog = LogManager.GetLogger("Example");
//log using native
log.LogInformation($"log:Info"); //appears in live-stream, app-insights
log.LogError("log:Error"); //appears in live-stream, app-insights
log.LogTrace("log:Trace"); //appears in live-stream, app-insights (after modifying host.json)
//log using nlog
nlog.Info("nlog:info"); //appears in ........... app-insights
nlog.Error("nlog:error"); //appears in ........... app-insights
nlog.Trace("nlog:trace"); //appears in ........... app-insights
//say goodbye
log.LogInformation("log:ending");
}
}
Thanks in advance for any suggestions - no doubt I'm missing some simple step.