Skip to content

Commit d98f066

Browse files
author
Christoph Bühler
committed
fix: use environment to determine production logging
This fixes #18.
1 parent 3a397d8 commit d98f066

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

src/KubeOps/Operator/KubernetesOperator.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Reflection;
45
using System.Threading.Tasks;
56
using k8s;
67
using KubeOps.Operator.Caching;
78
using KubeOps.Operator.Client;
89
using KubeOps.Operator.Commands;
910
using KubeOps.Operator.DependencyInjection;
11+
using KubeOps.Operator.Logging;
1012
using KubeOps.Operator.Queue;
1113
using KubeOps.Operator.Serialization;
1214
using KubeOps.Operator.Watcher;
@@ -19,11 +21,6 @@
1921
using Newtonsoft.Json.Serialization;
2022
using YamlDotNet.Serialization;
2123

22-
#if !DEBUG
23-
using System.Linq;
24-
using KubeOps.Operator.Logging;
25-
#endif
26-
2724
namespace KubeOps.Operator
2825
{
2926
public sealed class KubernetesOperator
@@ -58,25 +55,29 @@ public Task<int> Run(string[] args)
5855
var app = new CommandLineApplication<RunOperator>();
5956

6057
_builder.ConfigureLogging(
61-
builder =>
58+
(hostContext, logging) =>
6259
{
63-
builder.ClearProviders();
64-
#if DEBUG
65-
builder.AddConsole(options => options.TimestampFormat = @"[HH:mm:ss] ");
66-
#else
67-
if (args.Contains(NoStructuredLogs))
60+
logging.ClearProviders();
61+
62+
if (hostContext.HostingEnvironment.IsProduction())
6863
{
69-
builder.AddConsole(options =>
64+
if (args.Contains(NoStructuredLogs))
7065
{
71-
options.TimestampFormat = @"[dd.MM.yyyy - HH:mm:ss] ";
72-
options.DisableColors = true;
73-
});
66+
logging.AddConsole(options =>
67+
{
68+
options.TimestampFormat = @"[dd.MM.yyyy - HH:mm:ss] ";
69+
options.DisableColors = true;
70+
});
71+
}
72+
else
73+
{
74+
logging.AddStructuredConsole();
75+
}
7476
}
7577
else
7678
{
77-
builder.AddStructuredConsole();
79+
logging.AddConsole(options => options.TimestampFormat = @"[HH:mm:ss] ");
7880
}
79-
#endif
8081
});
8182

8283
var host = _builder.Build();
@@ -109,7 +110,7 @@ private void ConfigureRequiredServices() =>
109110
{
110111
ContractResolver = new NamingConvention(),
111112
Converters = new List<JsonConverter>
112-
{ new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy() } },
113+
{new StringEnumConverter {NamingStrategy = new CamelCaseNamingStrategy()}},
113114
});
114115
services.AddTransient(
115116
_ => new SerializerBuilder()
@@ -130,13 +131,13 @@ private void ConfigureRequiredServices() =>
130131
{
131132
ContractResolver = new NamingConvention(),
132133
Converters = new List<JsonConverter>
133-
{ new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy() } }
134+
{new StringEnumConverter {NamingStrategy = new CamelCaseNamingStrategy()}}
134135
},
135136
DeserializationSettings =
136137
{
137138
ContractResolver = new NamingConvention(),
138139
Converters = new List<JsonConverter>
139-
{ new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy() } }
140+
{new StringEnumConverter {NamingStrategy = new CamelCaseNamingStrategy()}}
140141
}
141142
};
142143
});

0 commit comments

Comments
 (0)