public LogRecordTest()
        {
            this.exporter  = new InMemoryExporter <LogRecord>(this.exportedItems);
            this.processor = new TestLogRecordProcessor(this.exporter);
#if NETCOREAPP2_1
            var serviceCollection = new ServiceCollection().AddLogging(builder =>
#else
            this.loggerFactory = LoggerFactory.Create(builder =>
#endif
            {
                builder.AddOpenTelemetry(options =>
                {
                    this.options = options;
                    options
                    .AddProcessor(this.processor);
                });
                builder.AddFilter(typeof(LogRecordTest).FullName, LogLevel.Trace);
            });

#if NETCOREAPP2_1
            this.serviceProvider = serviceCollection.BuildServiceProvider();
            this.logger          = this.serviceProvider.GetRequiredService <ILogger <LogRecordTest> >();
#else
            this.logger = this.loggerFactory.CreateLogger <LogRecordTest>();
#endif
        }
Example #2
0
    public static OpenTelemetryLoggerOptions AddMyExporter(this OpenTelemetryLoggerOptions options)
    {
        if (options == null)
        {
            throw new ArgumentNullException(nameof(options));
        }

        return(options.AddProcessor(new BatchExportProcessor <LogRecord>(new MyExporter())));
    }
Example #3
0
        /// <summary>
        /// Adds Console Exporter as a configuration to the OpenTelemetry ILoggingBuilder.
        /// </summary>
        /// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
        /// <param name="configure">Exporter configuration options.</param>
        /// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
        public static OpenTelemetryLoggerOptions AddConsoleExporter(this OpenTelemetryLoggerOptions loggerOptions, Action <ConsoleExporterOptions> configure = null)
        {
            if (loggerOptions == null)
            {
                throw new ArgumentNullException(nameof(loggerOptions));
            }

            var options = new ConsoleExporterOptions();

            configure?.Invoke(options);
            return(loggerOptions.AddProcessor(new SimpleExportProcessor <LogRecord>(new ConsoleExporter <LogRecord>(options))));
        }
        public static OpenTelemetryLoggerOptions AddAzureMonitorLogExporter(this OpenTelemetryLoggerOptions loggerOptions, Action <AzureMonitorExporterOptions> configure = null)
        {
            if (loggerOptions == null)
            {
                throw new ArgumentNullException(nameof(loggerOptions));
            }

            var options = new AzureMonitorExporterOptions();

            configure?.Invoke(options);

            return(loggerOptions.AddProcessor(new BatchLogRecordExportProcessor(new AzureMonitorLogExporter(options))));
        }
        public static OpenTelemetryLoggerOptions AddInMemoryExporter(this OpenTelemetryLoggerOptions loggerOptions, ICollection <LogRecord> exportedItems)
        {
            if (loggerOptions == null)
            {
                throw new ArgumentNullException(nameof(loggerOptions));
            }

            if (exportedItems == null)
            {
                throw new ArgumentNullException(nameof(exportedItems));
            }

            return(loggerOptions.AddProcessor(new SimpleExportProcessor <LogRecord>(new InMemoryExporter <LogRecord>(exportedItems))));
        }
Example #6
0
        public LogRecordTest()
        {
            this.exporter      = new InMemoryExporter <LogRecord>(this.exportedItems);
            this.processor     = new TestLogRecordProcessor(this.exporter);
            this.loggerFactory = LoggerFactory.Create(builder =>
            {
                builder.AddOpenTelemetry(options =>
                {
                    this.options = options;
                    options
                    .AddProcessor(this.processor);
                });
                builder.AddFilter(typeof(LogRecordTest).FullName, LogLevel.Trace);
            });

            this.logger = this.loggerFactory.CreateLogger <LogRecordTest>();
        }
Example #7
0
        /// <summary>
        /// Adds a <see cref="LogRecord"/> processor to the OpenTelemetry <see
        /// cref="OpenTelemetryLoggerOptions"/> which will convert log messages
        /// into <see cref="ActivityEvent"/>s attached to the active <see
        /// cref="Activity"/> when the message is written.
        /// </summary>
        /// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
        /// <param name="configure"><see cref="LogToActivityEventConversionOptions"/>.</param>
        /// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="loggerOptions"/> is <c>null</c>.</exception>
        public static OpenTelemetryLoggerOptions AttachLogsToActivityEvent(
            this OpenTelemetryLoggerOptions loggerOptions,
            Action <LogToActivityEventConversionOptions>?configure = null)
        {
            if (loggerOptions == null)
            {
                throw new ArgumentNullException(nameof(loggerOptions));
            }

            var options = new LogToActivityEventConversionOptions();

            configure?.Invoke(options);
#pragma warning disable CA2000 // Dispose objects before losing scope
            return(loggerOptions.AddProcessor(new ActivityEventAttachingLogProcessor(options)));

#pragma warning restore CA2000 // Dispose objects before losing scope
        }
        /// <summary>
        /// Adds Azure Monitor Log Exporter with OpenTelemetryLoggerOptions.
        /// </summary>
        /// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
        /// <param name="configure">Exporter configuration options.</param>
        /// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
        public static OpenTelemetryLoggerOptions AddAzureMonitorLogExporter(this OpenTelemetryLoggerOptions loggerOptions, Action <AzureMonitorExporterOptions> configure = null)
        {
            if (loggerOptions == null)
            {
                throw new ArgumentNullException(nameof(loggerOptions));
            }

            var options = new AzureMonitorExporterOptions();

            configure?.Invoke(options);

            // TODO: Fallback to default location if location provided via options does not work.
            if (!options.DisableOfflineStorage && options.StorageDirectory == null)
            {
                options.StorageDirectory = StorageHelper.GetDefaultStorageDirectory();
            }

            return(loggerOptions.AddProcessor(new BatchLogRecordExportProcessor(new AzureMonitorLogExporter(options))));
        }