### Verify Migration Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/MIGRATION.md Run TypeScript compiler checks to ensure no type errors remain after migration. ```bash tsc --noEmit --composite false --skipLibCheck ``` -------------------------------- ### Install Winston Azure Application Insights Transport Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/README.md Installs the @shellicar/winston-azure-application-insights package using pnpm. This is the first step to integrating Winston logging with Azure Application Insights. ```sh pnpm add @shellicar/winston-azure-application-insights ``` -------------------------------- ### Update Package Dependency Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/MIGRATION.md Command to upgrade the package to the latest major version using pnpm. ```bash pnpm add @shellicar/winston-azure-application-insights@^6 ``` -------------------------------- ### Migrate Configuration Options Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/MIGRATION.md Update configuration by removing deprecated flags and moving options to their new nested locations. ```typescript // Remove this option (functionality is now automatic): sendErrorsAsExceptions: true, // Delete - now always enabled // Move these options to new locations: silent: false, // Move to winston.insights.enabled: true defaultLevel: 'info', // Move to winston.defaults.level: 'info' ``` -------------------------------- ### Migrating Winston Logger Factory Configuration (v5.x to v6.x) Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/MIGRATION.md This snippet demonstrates the migration of the logger factory configuration from v5.x to v6.x. It highlights changes in import statements, filter definitions, and the structure of the configuration object for both Application Insights and Winston settings. Version 6.x introduces enums for versions and severity mapping, and a more structured approach to Winston configuration. ```typescript import { createWinstonLogger, type ITelemetryFilterV3 } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; import winston from 'winston'; applicationinsights.setup().start(); const filter = { filterException(trace) { console.log('Filtering exception:', trace); return true; }, filterTrace(trace) { console.log('Filtering trace:', trace); return true; }, } satisfies ITelemetryFilterV3; const logger = createWinstonLogger({ insights: { client: applicationinsights.defaultClient, version: 3, defaultLevel: 'info', filters: [filter], levels: { // info -> error info: 3, // error -> critical error: 4, }, sendErrorsAsExceptions: true, silent: false, }, winston: { console: true, defaultMeta: { myDefault: 'meta', }, format: [winston.format.timestamp(), winston.format.errors(), winston.format.json()], level: 'verbose', levels: winston.config.npm.levels, }, }); logger.info('This is an info message'); logger.error('This is an error message', new Error('Test error')); ``` ```typescript import { ApplicationInsightsVersion, createWinstonLogger, type IExceptionTelemetryFilter, type ITraceTelemetryFilter, TelemetrySeverity } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; import winston from 'winston'; applicationinsights.setup().start(); const traceFilter: ITraceTelemetryFilter = (trace) => { console.log('Filtering trace:', trace); return true; }; const exceptionFilter: IExceptionTelemetryFilter = (exception) => { console.log('Filtering exception:', exception); return true; }; const logger = createWinstonLogger({ insights: { client: applicationinsights.defaultClient, version: ApplicationInsightsVersion.V3, severityMapping: { info: TelemetrySeverity.Error, error: TelemetrySeverity.Critical, }, exceptionFilter, traceFilter, }, winston: { console: { enabled: true, format: { output: 'json', colorize: true, errors: true, timestamp: true, }, }, defaults: { defaultMeta: { myDefault: 'meta', }, level: 'verbose', }, insights: { level: 'info', }, levels: winston.config.npm.levels, }, }); logger.info('This is an info message'); logger.error('This is an error message', new Error('Test error')); ``` -------------------------------- ### Install Winston Azure Application Insights Transport Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/packages/winston-azure-application-insights/README.md Installs the @shellicar/winston-azure-application-insights package using npm. This is the first step to integrate the transport into your project. ```shell npm install @shellicar/winston-azure-application-insights ``` -------------------------------- ### Migrating Winston Transport Configuration (v5.x to v6.x) Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/MIGRATION.md This snippet illustrates the migration of a transport-only configuration for Azure Application Insights from v5.x to v6.x. It shows how to update imports, replace the `AzureApplicationInsightsLogger` class with the `createApplicationInsightsTransport` function, and adapt the configuration object, including the new `severityMapping` and separate `traceFilter` and `exceptionFilter` properties. ```typescript import { AzureApplicationInsightsLogger, type ITelemetryFilterV3 } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; applicationinsights.setup().start(); const filter = { filterException(trace) { console.log('Filtering exception:', trace); return true; }, filterTrace(trace) { console.log('Filtering trace:', trace); return true; }, } satisfies ITelemetryFilterV3; const transport = new AzureApplicationInsightsLogger({ client: applicationinsights.defaultClient, version: 3, defaultLevel: 'info', filters: [filter], levels: { // info -> error info: 3, // error -> critical error: 4, }, sendErrorsAsExceptions: true, silent: false, }); ``` ```typescript import { ApplicationInsightsVersion, createApplicationInsightsTransport, type IExceptionTelemetryFilter, type ITraceTelemetryFilter, TelemetrySeverity } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; applicationinsights.setup().start(); const traceFilter: ITraceTelemetryFilter = (trace) => { console.log('Filtering trace:', trace); return true; }; const exceptionFilter: IExceptionTelemetryFilter = (exception) => { console.log('Filtering exception:', exception); return true; }; const transport = createApplicationInsightsTransport({ client: applicationinsights.defaultClient, version: ApplicationInsightsVersion.V3, severityMapping: { info: TelemetrySeverity.Error, error: TelemetrySeverity.Critical, }, exceptionFilter, traceFilter, level: 'info', }); ``` -------------------------------- ### Update Version Parameter Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/MIGRATION.md Replace numeric version constants with the new ApplicationInsightsVersion enum. ```typescript // Change version: 3, // To version: ApplicationInsightsVersion.V3, ``` -------------------------------- ### Split Filter Functions Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/MIGRATION.md Transition from a single filter object to separate trace and exception filter functions. ```typescript // Change const filter: ITelemetryFilterV3 = { filterTrace: (trace) => true, filterException: (exception) => true, }; filters: [filter], // To const traceFilter: ITraceTelemetryFilter = (trace) => true; const exceptionFilter: IExceptionTelemetryFilter = (exception) => true; ``` -------------------------------- ### Quick Start: Winston Logging to Azure Application Insights (TypeScript) Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/packages/winston-azure-application-insights/README.md Demonstrates how to set up and use the Winston Azure Application Insights transport. It initializes Application Insights, creates a Winston logger with the transport, and logs informational and error messages. Requires 'applicationinsights' and '@shellicar/winston-azure-application-insights' packages. ```typescript import { createWinstonLogger, ApplicationInsightsVersion } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; applicationinsights.setup().start(); const logger = createWinstonLogger({ insights: { version: ApplicationInsightsVersion.V3, client: applicationinsights.defaultClient }, }); logger.info('Hello World'); logger.error('Something went wrong', new Error('Database connection failed')); ``` -------------------------------- ### Detect Local Environment with isRunningLocally Source: https://context7.com/shellicar/winston-azure-application-insights/llms.txt Utility function to identify if the application is running in a local development setup, as opposed to an Azure environment. This function is useful for conditionally configuring logging behavior, such as enabling console output only locally and sending telemetry to Application Insights only in production. ```typescript import { ApplicationInsightsVersion, createWinstonLogger, isRunningLocally } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; // Conditional Application Insights setup if (!isRunningLocally()) { applicationinsights.setup().start(); } const logger = createWinstonLogger({ winston: { console: { enabled: isRunningLocally(), // Only show console locally format: { output: 'simple', colorize: true, }, }, insights: { enabled: !isRunningLocally(), // Only send to AI in production }, }, insights: { version: ApplicationInsightsVersion.V3, client: isRunningLocally() ? undefined : applicationinsights.defaultClient, telemetryHandler: isRunningLocally() ? { handleTelemetry: () => {} } : undefined, }, }); logger.info('Environment-aware logging configured'); ``` -------------------------------- ### Update Severity Mapping Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/MIGRATION.md Replace numeric levels with the TelemetrySeverity enum for better type safety. ```typescript // Change levels: { info: 3, }, // To severityMapping: { info: TelemetrySeverity.Error, }, ``` -------------------------------- ### Update TypeScript Imports Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/MIGRATION.md Refactor imports to remove deprecated interfaces and include new enums and filter types. ```typescript // Remove import { ITelemetryFilterV3 } from '@shellicar/winston-azure-application-insights'; // Add import { ApplicationInsightsVersion, TelemetrySeverity, ITraceTelemetryFilter, IExceptionTelemetryFilter } from '@shellicar/winston-azure-application-insights'; ``` -------------------------------- ### Update Winston Configuration Structure Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/MIGRATION.md Restructure the winston configuration object to use nested objects for console, defaults, and insights settings. ```typescript winston: { console: { enabled: true }, defaults: { defaultMeta: { ... }, level: 'info' }, insights: { enabled: true } } ``` -------------------------------- ### Configure Complete Winston Logger Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/README.md Shows how to instantiate a full Winston logger configured with both console output and the Application Insights transport. ```typescript import { createWinstonLogger, ApplicationInsightsVersion } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; const logger = createWinstonLogger({ winston: { console: { enabled: true, format: { output: 'json', timestamp: true, errors: true, colorize: true, }, }, defaults: { level: 'info', }, }, insights: { version: ApplicationInsightsVersion.V3, client: applicationinsights.defaultClient, }, }); logger.info('Application started'); ``` -------------------------------- ### Configure Winston Transport with Existing Application Insights Client Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/README.md Demonstrates how to initialize the Winston transport by providing an existing Application Insights client instance, which is useful when the SDK is already initialized in the environment to avoid duplicate registration errors. ```typescript import applicationinsights from 'applicationinsights'; const transport = createApplicationInsightsTransport({ version: ApplicationInsightsVersion.V3, client: applicationinsights.defaultClient, }); ``` -------------------------------- ### Create Winston Logger with Azure Application Insights Transport (TypeScript) Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/README.md Demonstrates how to create a Winston logger instance configured with the Azure Application Insights transport. It initializes the Application Insights SDK and uses a factory function to create the transport, specifying the SDK version and client. The logger then sends an 'Hello World' message. ```typescript import { createWinstonLogger, ApplicationInsightsVersion } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; applicationinsights.setup().start(); const logger = createWinstonLogger({ insights: { version: ApplicationInsightsVersion.V3, client: applicationinsights.defaultClient }, }); logger.info('Hello World'); ``` -------------------------------- ### Initialize Application Insights Transport Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/README.md Demonstrates how to create a transport instance for Winston using the factory function. It requires the Application Insights SDK client and a specified version. ```typescript import applicationinsights from 'applicationinsights'; import { createApplicationInsightsTransport, ApplicationInsightsVersion } from '@shellicar/winston-azure-application-insights'; applicationinsights.setup().start(); const transport = createApplicationInsightsTransport({ version: ApplicationInsightsVersion.V3, client: applicationinsights.defaultClient, }); ``` -------------------------------- ### Configure Error Handling and Exception Tracking in Winston Source: https://context7.com/shellicar/winston-azure-application-insights/llms.txt Demonstrates how the library automatically extracts Error objects from log calls. It shows how to handle scenarios where an Error is the primary argument versus when it is included alongside other metadata. ```typescript import { ApplicationInsightsVersion, createWinstonLogger } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; applicationinsights.setup().start(); const logger = createWinstonLogger({ insights: { version: ApplicationInsightsVersion.V3, client: applicationinsights.defaultClient, }, }); // Creates trace only (no Error object) logger.error('Something went wrong'); // Creates EXCEPTION ONLY (no trace) - when first parameter is Error logger.error(new Error('Database connection failed')); // Creates trace + exception (Error extracted from additional parameters) logger.error('Operation failed', new Error('Timeout exceeded')); // Creates trace + multiple exceptions logger.error('Multiple failures occurred', new Error('DB error'), new Error('Cache error')); // Mixed types - Error objects extracted, others become properties logger.error('Complex operation failed', { userId: 123, operation: 'checkout' }, new Error('Payment failed')); ``` -------------------------------- ### Configure Separate Transport Levels in TypeScript Source: https://context7.com/shellicar/winston-azure-application-insights/llms.txt Explains how to set different logging thresholds for console output versus Azure Application Insights. This allows for verbose local development logs while maintaining a cleaner, higher-severity log stream in production. ```typescript import { ApplicationInsightsVersion, createWinstonLogger } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; const logger = createWinstonLogger({ insights: { version: ApplicationInsightsVersion.V3, client: applicationinsights.defaultClient }, winston: { defaults: { level: 'verbose' }, console: { level: 'verbose' }, insights: { level: 'info' }, }, }); ``` -------------------------------- ### Create Winston Logger with Factory Function Source: https://context7.com/shellicar/winston-azure-application-insights/llms.txt Uses the createWinstonLogger factory to initialize a logger with both console and Application Insights transports. It handles default metadata, environment configuration, and SDK versioning. ```typescript import { ApplicationInsightsVersion, createWinstonLogger } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; // Initialize Application Insights first applicationinsights.setup().start(); const logger = createWinstonLogger({ winston: { console: { enabled: true, format: { output: 'json', timestamp: true, errors: true, colorize: true, }, }, defaults: { level: 'info', defaultMeta: { service: 'my-app', environment: 'production' }, }, }, insights: { version: ApplicationInsightsVersion.V3, client: applicationinsights.defaultClient, }, }); // Basic logging logger.info('Application started'); logger.warn('Configuration missing, using defaults'); logger.error('Something went wrong', new Error('Database connection failed')); // With metadata logger.info('User logged in', { userId: 123, action: 'login' }); ``` -------------------------------- ### Implement Custom Telemetry Handler in TypeScript Source: https://context7.com/shellicar/winston-azure-application-insights/llms.txt Demonstrates how to create a custom telemetry handler to intercept, process, or store telemetry data before it is sent to Azure. This is useful for testing, debugging, or routing logs to secondary backends. ```typescript import { createWinstonLogger, type TelemetryData, type TelemetryHandler } from '@shellicar/winston-azure-application-insights'; class CustomTelemetryHandler implements TelemetryHandler { private telemetryLog: TelemetryData[] = []; handleTelemetry(telemetry: TelemetryData): void { this.telemetryLog.push(telemetry); if (telemetry.trace) { console.log(`[${telemetry.trace.severity}] ${telemetry.trace.message}`); } for (const exception of telemetry.exceptions) { console.error(`[EXCEPTION] ${exception.exception.message}`); } } getTelemetryLog(): TelemetryData[] { return this.telemetryLog; } } const telemetryHandler = new CustomTelemetryHandler(); const logger = createWinstonLogger({ insights: { telemetryHandler }, winston: { console: { enabled: false } }, }); ``` -------------------------------- ### Handle Error Extraction and Logging Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/README.md Explains how the logger automatically detects Error objects. Depending on the input, it logs either a trace, an exception, or both. ```typescript // Creates trace only logger.error('Something went wrong'); // Creates EXCEPTION ONLY (no trace) - when first parameter is Error logger.error(new Error('Database error')); // Creates trace + exception (Error extracted from additional parameters) logger.error('Operation failed', new Error('Timeout')); // Creates trace + two exceptions (multiple Error objects) logger.error('Multiple failures', new Error('DB error'), new Error('Cache error')); ``` -------------------------------- ### Support Multiple SDK Versions Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/README.md Configures the transport to work with either Application Insights v2 or v3 by specifying the version in the options. ```typescript // Application Insights v2 import applicationinsights from 'applicationinsights'; const transportV2 = createApplicationInsightsTransport({ version: ApplicationInsightsVersion.V2, client: applicationinsights.defaultClient, }); // Application Insights v3 const transportV3 = createApplicationInsightsTransport({ version: ApplicationInsightsVersion.V3, client: applicationinsights.defaultClient, }); ``` -------------------------------- ### Configure Winston Logger for Application Insights v2 Source: https://context7.com/shellicar/winston-azure-application-insights/llms.txt Sets up the Winston logger to use Azure Application Insights SDK v2. This is useful for legacy applications. It requires the '@shellicar/winston-azure-application-insights' package and the 'applicationinsights' SDK. ```typescript import { ApplicationInsightsVersion, createWinstonLogger } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; // Using Application Insights SDK v2.x applicationinsights.setup().start(); const logger = createWinstonLogger({ insights: { version: ApplicationInsightsVersion.V2, // Specify v2 client: applicationinsights.defaultClient, }, winston: { defaults: { defaultMeta: { sdkVersion: 'applicationinsights@2', }, }, }, }); logger.info('Running with Application Insights v2'); ``` -------------------------------- ### Detect Azure Environment with isRunningInAzure Source: https://context7.com/shellicar/winston-azure-application-insights/llms.txt A utility function to determine if the application is executing within an Azure environment, such as Azure App Service or Azure Functions. It checks for the presence of the WEBSITE_INSTANCE_ID environment variable. This helps in conditionally enabling production telemetry. ```typescript import { isRunningInAzure, isRunningLocally } from '@shellicar/winston-azure-application-insights'; if (isRunningInAzure()) { console.log('Running in Azure App Service or Azure Functions'); // Enable production telemetry } else { console.log('Running locally or in non-Azure environment'); // Use local development settings } // isRunningLocally is the inverse if (isRunningLocally()) { console.log('Local development mode'); } ``` -------------------------------- ### Extract Telemetry Properties Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/README.md Demonstrates how Winston splat parameters are automatically converted into telemetry properties within Application Insights. ```typescript // Simple properties logger.info('User logged in', { userId: 123, action: 'login' }); // Mixed types - Error objects are extracted, others become properties logger.error('Complex operation failed', { userId: 123, operation: 'checkout' }, new Error('Payment failed')); ``` -------------------------------- ### Map Severity Levels Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/README.md Shows default severity mapping from Winston levels to Application Insights levels, including custom overrides. ```typescript const transport = createApplicationInsightsTransport({ version: ApplicationInsightsVersion.V3, client: defaultClient, severityMapping: { error: TelemetrySeverity.Error, warn: TelemetrySeverity.Warning, info: TelemetrySeverity.Information, debug: TelemetrySeverity.Verbose, audit: TelemetrySeverity.Critical, security: TelemetrySeverity.Error, }, }); ``` -------------------------------- ### ApplicationInsightsVersion Enum Usage (TypeScript) Source: https://context7.com/shellicar/winston-azure-application-insights/llms.txt Demonstrates how to use the ApplicationInsightsVersion enum to specify the Application Insights SDK version for configuration. This is useful when initializing Application Insights for different SDK versions. ```typescript import { ApplicationInsightsVersion } from '@shellicar/winston-azure-application-insights'; // For Application Insights SDK 2.x const v2Config = { version: ApplicationInsightsVersion.V2, // "2.x" }; // For Application Insights SDK 3.x const v3Config = { version: ApplicationInsightsVersion.V3, // "3.x" }; ``` -------------------------------- ### Implement Exception Telemetry Filtering Source: https://context7.com/shellicar/winston-azure-application-insights/llms.txt Explains how to filter out expected exceptions or specific error patterns before they reach Application Insights. This helps reduce noise from known non-critical errors. ```typescript import { ApplicationInsightsVersion, createWinstonLogger, type IExceptionTelemetryFilter } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; applicationinsights.setup().start(); class ExpectedValidationError extends Error { constructor(message: string) { super(message); this.name = 'ExpectedValidationError'; } } const exceptionFilter: IExceptionTelemetryFilter = (exception) => { if (exception.exception instanceof ExpectedValidationError) { return false; } if (exception.exception.message.includes('ECONNRESET')) { return false; } if (exception.properties.expected === true) { return false; } return true; }; const logger = createWinstonLogger({ insights: { version: ApplicationInsightsVersion.V3, client: applicationinsights.defaultClient, exceptionFilter, }, }); ``` -------------------------------- ### Create Standalone Application Insights Transport Source: https://context7.com/shellicar/winston-azure-application-insights/llms.txt Creates a custom Winston transport for Application Insights, allowing for granular control over severity mapping and integration into existing Winston logger instances. ```typescript import { ApplicationInsightsVersion, createApplicationInsightsTransport, TelemetrySeverity } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; import winston from 'winston'; applicationinsights.setup().start(); // Create the Application Insights transport const aiTransport = createApplicationInsightsTransport({ version: ApplicationInsightsVersion.V3, client: applicationinsights.defaultClient, level: 'info', severityMapping: { error: TelemetrySeverity.Error, warn: TelemetrySeverity.Warning, info: TelemetrySeverity.Information, debug: TelemetrySeverity.Verbose, audit: TelemetrySeverity.Critical, // Custom level mapping }, }); // Create Winston logger with custom configuration const logger = winston.createLogger({ level: 'debug', format: winston.format.combine( winston.format.timestamp(), winston.format.errors({ stack: true }), winston.format.json() ), transports: [ new winston.transports.Console(), aiTransport, ], }); logger.info('Custom transport setup complete'); logger.audit('Security audit event', { userId: 456, action: 'admin_access' }); ``` -------------------------------- ### Map Custom Winston Levels to Azure Severity in TypeScript Source: https://context7.com/shellicar/winston-azure-application-insights/llms.txt Shows how to define custom Winston log levels and map them to specific Azure Application Insights TelemetrySeverity levels. This ensures that non-standard log levels are correctly categorized in the Azure portal. ```typescript import { ApplicationInsightsVersion, createApplicationInsightsTransport, TelemetrySeverity } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; import winston from 'winston'; const customLevels = { levels: { emergency: 0, alert: 1, critical: 2, error: 3, warning: 4, notice: 5, info: 6, debug: 7 } }; const transport = createApplicationInsightsTransport({ version: ApplicationInsightsVersion.V3, client: applicationinsights.defaultClient, severityMapping: { emergency: TelemetrySeverity.Critical, alert: TelemetrySeverity.Critical, critical: TelemetrySeverity.Critical, error: TelemetrySeverity.Error, warning: TelemetrySeverity.Warning, notice: TelemetrySeverity.Information, info: TelemetrySeverity.Information, debug: TelemetrySeverity.Verbose, }, }); const logger = winston.createLogger({ levels: customLevels.levels, transports: [transport], }); ``` -------------------------------- ### Customize Error Detection Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/README.md Provides a custom function to determine which objects should be treated as errors for exception tracking. ```typescript const transport = createApplicationInsightsTransport({ version: 3, client: defaultClient, isError: (obj) => obj instanceof CustomError, }); ``` -------------------------------- ### Implement Trace Telemetry Filtering Source: https://context7.com/shellicar/winston-azure-application-insights/llms.txt Shows how to define a custom filter function to prevent specific traces from being sent to Application Insights. Filters can be based on severity, message content, or custom properties. ```typescript import { ApplicationInsightsVersion, createWinstonLogger, type ITraceTelemetryFilter, TelemetrySeverity } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; applicationinsights.setup().start(); const traceFilter: ITraceTelemetryFilter = (trace) => { if (trace.severity === TelemetrySeverity.Verbose) { return false; } if (trace.message.includes('health-check')) { return false; } if (trace.properties.internal === true) { return false; } return true; }; const logger = createWinstonLogger({ insights: { version: ApplicationInsightsVersion.V3, client: applicationinsights.defaultClient, traceFilter, }, }); ``` -------------------------------- ### Filter Telemetry Data Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/README.md Implements custom filtering logic for traces and exceptions to control what data is sent to Application Insights. ```typescript const transport = createApplicationInsightsTransport({ version: ApplicationInsightsVersion.V3, client: defaultClient, traceFilter: (trace) => trace.severity !== KnownSeverityLevel.Verbose, exceptionFilter: (exception) => !exception.exception.message.includes('ignore'), }); ``` -------------------------------- ### Disable Automatic Console Collection Source: https://github.com/shellicar/winston-azure-application-insights/blob/main/README.md Shows how to disable the default Application Insights auto-collection of console logs to prevent duplicate entries when using a custom Winston transport. ```typescript setup().setAutoCollectConsole(false).start(); ``` -------------------------------- ### Custom Error Detection for Application Insights Source: https://context7.com/shellicar/winston-azure-application-insights/llms.txt Allows overriding the default error detection logic in Application Insights transport. This enables handling custom error types like GraphQLError or excluding specific objects from being logged as errors. It requires 'applicationinsights' and 'winston' packages. ```typescript import { ApplicationInsightsVersion, createApplicationInsightsTransport } from '@shellicar/winston-azure-application-insights'; import applicationinsights from 'applicationinsights'; import winston from 'winston'; applicationinsights.setup().start(); // Custom error class class GraphQLError extends Error { constructor(message: string, public code: string, public path: string[]) { super(message); this.name = 'GraphQLError'; } } // Custom error detection that includes GraphQL errors const isError = (obj: unknown): obj is Error => { if (obj instanceof Error) return true; if (obj instanceof GraphQLError) return true; // Check for error-like objects if (typeof obj === 'object' && obj !== null && 'message' in obj && 'stack' in obj) { return true; } return false; }; const transport = createApplicationInsightsTransport({ version: ApplicationInsightsVersion.V3, client: applicationinsights.defaultClient, isError, }); const logger = winston.createLogger({ transports: [transport], }); const gqlError = new GraphQLError('Field not found', 'FIELD_NOT_FOUND', ['user', 'email']); logger.error('GraphQL query failed', gqlError); // GraphQL error tracked as exception ``` -------------------------------- ### TelemetrySeverity Enum for Application Insights Source: https://context7.com/shellicar/winston-azure-application-insights/llms.txt Defines an enumeration of Application Insights severity levels. This enum is intended for use when mapping log levels to Application Insights severity or when implementing custom filters for telemetry data. It provides constants for standard severity levels. ```typescript import { TelemetrySeverity } from '@shellicar/winston-azure-application-insights'; // Available severity levels const severities = { verbose: TelemetrySeverity.Verbose, // "Verbose" information: TelemetrySeverity.Information, // "Information" warning: TelemetrySeverity.Warning, // "Warning" error: TelemetrySeverity.Error, // "Error" critical: TelemetrySeverity.Critical, // "Critical" }; // Use in severity mapping const severityMapping = { silly: TelemetrySeverity.Verbose, debug: TelemetrySeverity.Verbose, verbose: TelemetrySeverity.Verbose, info: TelemetrySeverity.Information, warn: TelemetrySeverity.Warning, error: TelemetrySeverity.Error, fatal: TelemetrySeverity.Critical, }; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.