microsoft / ApplicationInsights-node.js

Microsoft Application Insights SDK for Node.js
MIT License
324 stars 141 forks source link

Azure Functions console logging recorded as individual App Insights entries for every line break #1130

Open mdmower opened 1 year ago

mdmower commented 1 year ago

When using context.log.error() in an Azure Function to log a message, the message field in Application Insights entries is allowed to contain line breaks. When this applicationsinsights package is used to intercept console.error() messages, every line break results in an independent entry in Application Insights. For example, consider this simple HTTP trigger function (Node.js):

import { Context, HttpRequest } from "@azure/functions";

module.exports = function (context: Context, request: HttpRequest): void {
  try {
    throw new Error("Nope, not today.");
  } catch (e) {
    if (e instanceof Error) {
      console.error(`Failed to handle request\nURL: ${request.url}\n${e.stack || ""}`);
    }
  }
  context.res = {};
  context.done();
};

The resulting Application Insights log entries are split and the severity level assigned to each also varies: image

For comparison, here's the single entry generated when context.log.error() is used instead of console.error(): image