From dfb0aaeb5f71445928f6d2b0d0410b654c9749c6 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Wed, 7 Feb 2024 15:46:17 +0100 Subject: [PATCH] More initial logging --- .../Diagnostics/LogFileWriter.cs | 79 +++++++++++++++---- 1 file changed, 64 insertions(+), 15 deletions(-) diff --git a/src/Elastic.OpenTelemetry/Diagnostics/LogFileWriter.cs b/src/Elastic.OpenTelemetry/Diagnostics/LogFileWriter.cs index cd64a8c..8d3fb0a 100644 --- a/src/Elastic.OpenTelemetry/Diagnostics/LogFileWriter.cs +++ b/src/Elastic.OpenTelemetry/Diagnostics/LogFileWriter.cs @@ -5,6 +5,7 @@ using System.Runtime.InteropServices; using System.Text; using System.Threading.Channels; +using System.Xml.Serialization; namespace Elastic.OpenTelemetry.Diagnostics; @@ -55,27 +56,75 @@ private LogFileWriter() } }); - string[] preAmble = [ - $"Elastic OpenTelemetry Distribution: {Agent.InformationalVersion}", - $"Process ID: {process.Id}", - $"Process started: {process.StartTime.ToUniversalTime():yyyy-MM-dd HH:mm:ss.fff}", - ]; - var builder = StringBuilderCache.Acquire(); - foreach (var item in preAmble) + // Preamble and configuration entries are ALWAYS logged, regardless of the configured log level + LogPreamble(process, builder, _streamWriter); + LogConfiguration(builder, _streamWriter); + + StringBuilderCache.Release(builder); + + _streamWriter.AutoFlush = true; // Ensure we don't lose logs by not flushing to the file. + + static void LogPreamble(Process process, StringBuilder stringBuilder, StreamWriter streamWriter) { - // These preamble entries are ALWAYS logged, regardless of the configured log level - WriteLogPrefix(LogLevel.Info, builder); - builder.Append(item); - _streamWriter.WriteLine(builder.ToString()); - builder.Clear(); + string[] preAmble = [ + $"Elastic OpenTelemetry Distribution: {Agent.InformationalVersion}", + $"Process ID: {process.Id}", + $"Process name: {process.ProcessName}", + $"Process path: {Environment.ProcessPath}", + $"Process started: {process.StartTime.ToUniversalTime():yyyy-MM-dd HH:mm:ss.fff}", + $"Machine name: {Environment.MachineName}", + $"Process username: {Environment.UserName}", + $"User domain name: {Environment.UserDomainName}", + $"Command line: {Environment.CommandLine}", + $"Command current directory: {Environment.CurrentDirectory}", + $"Processor count: {Environment.ProcessorCount}", + $"OS version: {Environment.OSVersion}", + $"CLR version: {Environment.Version}", + ]; + + foreach (var item in preAmble) + { + WriteLogPrefix(LogLevel.Info, stringBuilder); + stringBuilder.Append(item); + streamWriter.WriteLine(stringBuilder.ToString()); + stringBuilder.Clear(); + } + + streamWriter.Flush(); } - _streamWriter.Flush(); - _streamWriter.AutoFlush = true; // Ensure we don't lose logs by not flushing to the file. + static void LogConfiguration(StringBuilder stringBuilder, StreamWriter streamWriter) + { + string[] environmentVariables = [ + EnvironmentVariables.ElasticOtelFileLogging, + EnvironmentVariables.ElasticOtelLogDirectoryEnvironmentVariable, + EnvironmentVariables.ElasticOtelLogLevelEnvironmentVariable + ]; - StringBuilderCache.Release(builder); + foreach (var variable in environmentVariables) + { + var envVarValue = Environment.GetEnvironmentVariable(variable); + + WriteLogPrefix(LogLevel.Info, stringBuilder); + + if (!string.IsNullOrEmpty(envVarValue)) + { + stringBuilder.Append($"Environment variable '{variable}' = '{envVarValue}'."); + streamWriter.WriteLine(stringBuilder.ToString()); + } + else + { + stringBuilder.Append($"Environment variable '{variable}' is not configured."); + streamWriter.WriteLine(stringBuilder.ToString()); + } + + stringBuilder.Clear(); + } + + streamWriter.Flush(); + } } private static readonly LogLevel ConfiguredLogLevel = GetConfiguredLogLevel();