ILogger

Brett Samblanet edited this page Jun 13, 2017 · 4 revisions

Functions has support for logging through the Microsoft.Extensions.Logging.ILogger interface. At a high level, this is not much different than logging via the TraceWriter: logs continue to go to the file system and will also go to Application Insights (currently in Preview) if the APPINSIGHTS_INSTRUMENTATIONKEY app setting is set. The main advantage of using ILogger is that you get support for structured logging via Application Insights, which allows for richer Analytics support.

To use ILogger as your logging interface, simply add a parameter to your function signature and use any of the logger extensions:

public static void Run(string myQueueItem, ILogger logger)
{    
    logger.LogInformation("C# Queue trigger function processed: {message}", myQueueItem);
}

Structured Logging

The ILogger extensions allow you pass strings with placeholders that are made available to any registered ILoggerProvider. This allows a provider to write more detailed, queryable log data (rather than just a simple string). If you look at the example above, the line logger.LogInformation("C# Queue trigger function processed: {myQueueItem}", myQueueItem) is not passing a formatted string; rather it is passing a string and an object. Behind-the-scenes, the logger formats the string for you but also provides a dictionary that contains a key message with the value from the myQueueItem parameter.

What this means for Functions is that the log message will be passed to Application Insights as a Trace message, with the placeholder dictionary added to the customDimensions field. The keys will be begin with prop__ to prevent any collisions with values that the Functions host may add directly to customDimensions. You can then query for these placeholders in Application Insights Analytics.

Note: Application Insights support is still in Preview, so the exact shape of this data in Application Insights is still being discussed and may change before Application Insights support hits GA.

{
  customDimensions: {
    "prop__{OriginalFormat}":"C# Queue trigger function processed: {message}",
    "Category":"Function",
    "prop__message":"c9519cbf-b1e6-4b9b-bf24-cb7d10b1bb89"
  }
  // snipping other values
}

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.