Ejemplo n.º 1
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception?exception, Func <TState, Exception?, string> formatter)
        {
            _ = formatter ?? throw new ArgumentNullException(nameof(formatter));

            if (!IsEnabled(logLevel))
            {
                return;
            }

            var configuredEventId = this.iotHubLoggerProvider.LoggerConfigurationMonitor.Configuration.EventId;

            if (configuredEventId == 0 || configuredEventId == eventId)
            {
                _ = Task.Run(async() =>
                {
                    try
                    {
                        var formattedMessage = LoggerHelper.AddScopeInformation(this.iotHubLoggerProvider.LoggerConfigurationMonitor.ScopeProvider, formatter(state, exception));
                        ModuleClient moduleClient;
                        try
                        {
                            moduleClient = await this.moduleClientFactory.Value;
                        }
                        catch (Exception)
                        {
                            this.hasError = true;
                            throw;
                        }

                        await SendAsync(moduleClient, formattedMessage);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Error when sending log to IoT Hub: {ex}");
                        throw;
                    }
                });
            }
        }