diff options
Diffstat (limited to 'src/csharp')
-rw-r--r-- | src/csharp/Grpc.Core.Tests/NUnitMain.cs | 2 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/GrpcEnvironment.cs | 2 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/Logging/LogLevel.cs | 6 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/Logging/LogLevelFilterLogger.cs | 43 | ||||
-rw-r--r-- | src/csharp/Grpc.Examples.Tests/NUnitMain.cs | 2 | ||||
-rw-r--r-- | src/csharp/Grpc.HealthCheck.Tests/NUnitMain.cs | 2 | ||||
-rw-r--r-- | src/csharp/Grpc.IntegrationTesting/NUnitMain.cs | 2 | ||||
-rw-r--r-- | src/csharp/Grpc.Microbenchmarks/Program.cs | 2 | ||||
-rw-r--r-- | src/csharp/Grpc.Reflection.Tests/NUnitMain.cs | 2 |
9 files changed, 55 insertions, 8 deletions
diff --git a/src/csharp/Grpc.Core.Tests/NUnitMain.cs b/src/csharp/Grpc.Core.Tests/NUnitMain.cs index 87972e23ad..49cb8cd3b9 100644 --- a/src/csharp/Grpc.Core.Tests/NUnitMain.cs +++ b/src/csharp/Grpc.Core.Tests/NUnitMain.cs @@ -33,7 +33,7 @@ namespace Grpc.Core.Tests public static int Main(string[] args) { // Make logger immune to NUnit capturing stdout and stderr to workaround https://github.com/nunit/nunit/issues/1406. - GrpcEnvironment.SetLogger(new TextWriterLogger(Console.Error)); + GrpcEnvironment.SetLogger(new ConsoleLogger()); #if NETCOREAPP1_0 return new AutoRun(typeof(NUnitMain).GetTypeInfo().Assembly).Execute(args, new ExtendedTextWrapper(Console.Out), Console.In); #else diff --git a/src/csharp/Grpc.Core/GrpcEnvironment.cs b/src/csharp/Grpc.Core/GrpcEnvironment.cs index cbc7d2078c..80031cb7ef 100644 --- a/src/csharp/Grpc.Core/GrpcEnvironment.cs +++ b/src/csharp/Grpc.Core/GrpcEnvironment.cs @@ -43,7 +43,7 @@ namespace Grpc.Core static readonly HashSet<Channel> registeredChannels = new HashSet<Channel>(); static readonly HashSet<Server> registeredServers = new HashSet<Server>(); - static ILogger logger = new NullLogger(); + static ILogger logger = new LogLevelFilterLogger(new ConsoleLogger(), LogLevel.Off, true); readonly GrpcThreadPool threadPool; readonly DebugStats debugStats = new DebugStats(); diff --git a/src/csharp/Grpc.Core/Logging/LogLevel.cs b/src/csharp/Grpc.Core/Logging/LogLevel.cs index 7718e3c2ab..4db0e47b97 100644 --- a/src/csharp/Grpc.Core/Logging/LogLevel.cs +++ b/src/csharp/Grpc.Core/Logging/LogLevel.cs @@ -39,6 +39,10 @@ namespace Grpc.Core.Logging /// <summary> /// Error severity. /// </summary> - Error + Error, + /// <summary> + /// Logging is off. + /// </summary> + Off = int.MaxValue } } diff --git a/src/csharp/Grpc.Core/Logging/LogLevelFilterLogger.cs b/src/csharp/Grpc.Core/Logging/LogLevelFilterLogger.cs index b611c191a5..a650d029d7 100644 --- a/src/csharp/Grpc.Core/Logging/LogLevelFilterLogger.cs +++ b/src/csharp/Grpc.Core/Logging/LogLevelFilterLogger.cs @@ -27,6 +27,8 @@ namespace Grpc.Core.Logging /// <summary>Logger that filters out messages below certain log level.</summary> public class LogLevelFilterLogger : ILogger { + // Verbosity environment variable used by C core. + private const string CoreVerbosityEnvVarName = "GRPC_VERBOSITY"; readonly ILogger innerLogger; readonly LogLevel logLevel; @@ -40,6 +42,19 @@ namespace Grpc.Core.Logging } /// <summary> + /// Creates and instance of <c>LogLevelFilter.</c> + /// The <c>fromEnvironmentVariable</c> parameter allows looking up "GRPC_VERBOSITY" setting provided by C-core + /// and uses the same log level for C# logs. Using this setting is recommended as it can prevent unintentionally hiding + /// C core logs requested by "GRPC_VERBOSITY" environment variable (which could happen if C# logger's log level was set to a more restrictive value). + /// </summary> + /// <param name="logger">the logger to forward filtered logs to.</param> + /// <param name="defaultLogLevel">the default log level, unless overriden by env variable.</param> + /// <param name="fromEnvironmentVariable">if <c>true</c>, override log level with setting from environment variable.</param> + public LogLevelFilterLogger(ILogger logger, LogLevel defaultLogLevel, bool fromEnvironmentVariable) : this(logger, GetLogLevelFromEnvironment(defaultLogLevel, fromEnvironmentVariable)) + { + } + + /// <summary> /// Returns a logger associated with the specified type. /// </summary> public virtual ILogger ForType<T>() @@ -141,5 +156,33 @@ namespace Grpc.Core.Logging innerLogger.Error(exception, message); } } + + /// <summary>Get log level based on a default and lookup of <c>GRPC_VERBOSITY</c> environment variable.</summary> + private static LogLevel GetLogLevelFromEnvironment(LogLevel defaultLogLevel, bool fromEnvironmentVariable) + { + if (!fromEnvironmentVariable) + { + return defaultLogLevel; + } + + var verbosityString = System.Environment.GetEnvironmentVariable(CoreVerbosityEnvVarName); + if (verbosityString == null) + { + return defaultLogLevel; + } + + // NOTE: C core doesn't have "WARNING" log level + switch (verbosityString.ToUpperInvariant()) + { + case "DEBUG": + return LogLevel.Debug; + case "INFO": + return LogLevel.Info; + case "ERROR": + return LogLevel.Error; + default: + return defaultLogLevel; + } + } } } diff --git a/src/csharp/Grpc.Examples.Tests/NUnitMain.cs b/src/csharp/Grpc.Examples.Tests/NUnitMain.cs index a83caea206..bcb8b46b64 100644 --- a/src/csharp/Grpc.Examples.Tests/NUnitMain.cs +++ b/src/csharp/Grpc.Examples.Tests/NUnitMain.cs @@ -33,7 +33,7 @@ namespace Grpc.Examples.Tests public static int Main(string[] args) { // Make logger immune to NUnit capturing stdout and stderr to workaround https://github.com/nunit/nunit/issues/1406. - GrpcEnvironment.SetLogger(new TextWriterLogger(Console.Error)); + GrpcEnvironment.SetLogger(new ConsoleLogger()); #if NETCOREAPP1_0 return new AutoRun(typeof(NUnitMain).GetTypeInfo().Assembly).Execute(args, new ExtendedTextWrapper(Console.Out), Console.In); #else diff --git a/src/csharp/Grpc.HealthCheck.Tests/NUnitMain.cs b/src/csharp/Grpc.HealthCheck.Tests/NUnitMain.cs index 57deb3ba30..365551e895 100644 --- a/src/csharp/Grpc.HealthCheck.Tests/NUnitMain.cs +++ b/src/csharp/Grpc.HealthCheck.Tests/NUnitMain.cs @@ -33,7 +33,7 @@ namespace Grpc.HealthCheck.Tests public static int Main(string[] args) { // Make logger immune to NUnit capturing stdout and stderr to workaround https://github.com/nunit/nunit/issues/1406. - GrpcEnvironment.SetLogger(new TextWriterLogger(Console.Error)); + GrpcEnvironment.SetLogger(new ConsoleLogger()); #if NETCOREAPP1_0 return new AutoRun(typeof(NUnitMain).GetTypeInfo().Assembly).Execute(args, new ExtendedTextWrapper(Console.Out), Console.In); #else diff --git a/src/csharp/Grpc.IntegrationTesting/NUnitMain.cs b/src/csharp/Grpc.IntegrationTesting/NUnitMain.cs index a36802af55..9d24762e0a 100644 --- a/src/csharp/Grpc.IntegrationTesting/NUnitMain.cs +++ b/src/csharp/Grpc.IntegrationTesting/NUnitMain.cs @@ -33,7 +33,7 @@ namespace Grpc.IntegrationTesting public static int Main(string[] args) { // Make logger immune to NUnit capturing stdout and stderr to workaround https://github.com/nunit/nunit/issues/1406. - GrpcEnvironment.SetLogger(new TextWriterLogger(Console.Error)); + GrpcEnvironment.SetLogger(new ConsoleLogger()); #if NETCOREAPP1_0 return new AutoRun(typeof(NUnitMain).GetTypeInfo().Assembly).Execute(args, new ExtendedTextWrapper(Console.Out), Console.In); #else diff --git a/src/csharp/Grpc.Microbenchmarks/Program.cs b/src/csharp/Grpc.Microbenchmarks/Program.cs index 8412ba3245..d07d4187c4 100644 --- a/src/csharp/Grpc.Microbenchmarks/Program.cs +++ b/src/csharp/Grpc.Microbenchmarks/Program.cs @@ -27,7 +27,7 @@ namespace Grpc.Microbenchmarks { public static void Main(string[] args) { - GrpcEnvironment.SetLogger(new TextWriterLogger(Console.Error)); + GrpcEnvironment.SetLogger(new ConsoleLogger()); var benchmark = new SendMessageBenchmark(); benchmark.Init(); foreach (int threadCount in new int[] {1, 1, 2, 4, 8, 12}) diff --git a/src/csharp/Grpc.Reflection.Tests/NUnitMain.cs b/src/csharp/Grpc.Reflection.Tests/NUnitMain.cs index a45dab8772..49ed1cc8d4 100644 --- a/src/csharp/Grpc.Reflection.Tests/NUnitMain.cs +++ b/src/csharp/Grpc.Reflection.Tests/NUnitMain.cs @@ -33,7 +33,7 @@ namespace Grpc.Reflection.Tests public static int Main(string[] args) { // Make logger immune to NUnit capturing stdout and stderr to workaround https://github.com/nunit/nunit/issues/1406. - GrpcEnvironment.SetLogger(new TextWriterLogger(Console.Error)); + GrpcEnvironment.SetLogger(new ConsoleLogger()); #if NETCOREAPP1_0 return new AutoRun(typeof(NUnitMain).GetTypeInfo().Assembly).Execute(args, new ExtendedTextWrapper(Console.Out), Console.In); #else |