public SimpleLoggerWriter() { // Look to see if Lambda's telemetry log file descriptor is available. If so use that for logging. // This will make sure multiline log messages use a single CloudWatch Logs record. var fileDescriptorLogId = Environment.GetEnvironmentVariable(Constants.ENVIRONMENT_VARIABLE_TELEMETRY_LOG_FD); if (fileDescriptorLogId != null) { try { _writer = FileDescriptorLogFactory.GetWriter(fileDescriptorLogId); InternalLogger.GetDefaultLogger().LogInformation("Using file descriptor stream writer for logging"); } catch (Exception ex) { _writer = Console.Out; InternalLogger.GetDefaultLogger().LogError(ex, "Error creating file descriptor log stream writer. Fallback to stdout."); } } else { _writer = Console.Out; InternalLogger.GetDefaultLogger().LogInformation("Using stdout for logging"); } }
/// <summary> /// Constructor used by bootstrap to put in place a wrapper TextWriter around stdout and stderror so all Console.Writeline calls /// will be formatted. /// /// Stdoud will default log messages to be Information /// Stderror will default log messages to be Error /// </summary> public LogLevelLoggerWriter() { // Look to see if Lambda's telemetry log file descriptor is available. If so use that for logging. // This will make sure multiline log messages use a single CloudWatch Logs record. var fileDescriptorLogId = Environment.GetEnvironmentVariable(Constants.ENVIRONMENT_VARIABLE_TELEMETRY_LOG_FD); if (fileDescriptorLogId != null) { try { var stdOutWriter = FileDescriptorLogFactory.GetWriter(fileDescriptorLogId); var stdErrorWriter = FileDescriptorLogFactory.GetWriter(fileDescriptorLogId); Initialize(stdOutWriter, stdErrorWriter); InternalLogger.GetDefaultLogger().LogInformation("Using file descriptor stream writer for logging."); } catch (Exception ex) { InternalLogger.GetDefaultLogger().LogError(ex, "Error creating file descriptor log stream writer. Fallback to stdout and stderr."); Initialize(Console.Out, Console.Error); } } else { Initialize(Console.Out, Console.Error); InternalLogger.GetDefaultLogger().LogInformation("Using stdout and stderr for logging."); } // SetOut will wrap our WrapperTextWriter with a synchronized TextWriter. Pass in the new synchronized // TextWriter into our writer to make sure we obtain a lock on that instance before writing to the stdout. Console.SetOut(_wrappedStdOutWriter); _wrappedStdOutWriter.LockObject = Console.Out; Console.SetError(_wrappedStdErrorWriter); _wrappedStdErrorWriter.LockObject = Console.Error; ConfigureLoggingActionField(); }