Skip to content

Commit ef26472

Browse files
committed
Add logging of web server events
Configuration: { "logLevel": "Warning" }
1 parent 0f3153b commit ef26472

5 files changed

Lines changed: 126 additions & 11 deletions

File tree

ArkBot/Modules/Application/Configuration/Model/Config.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,17 @@ public void SetupDefaults()
150150

151151
// Debugging, Logging etc.
152152

153+
[JsonProperty(PropertyName = "logLevel")]
154+
[Display(Name = "Log Level", Description = "Log level")]
155+
[DefaultValue(Microsoft.Extensions.Logging.LogLevel.Warning)]
156+
[Category(ConfigurationCategory.Debug)]
157+
[PropertyOrder(1)]
158+
public Microsoft.Extensions.Logging.LogLevel LogLevel { get; set; }
159+
153160
[JsonProperty(PropertyName = "anonymizeWebApiData")]
154161
[Display(Name = "Anonymize Web API Data", Description = "Anonymize all data in the WebAPI. Used to create data dumps for demoing the web-app")]
155162
[Category(ConfigurationCategory.Debug)]
156-
[PropertyOrder(1)]
163+
[PropertyOrder(2)]
157164
public bool AnonymizeWebApiData { get; set; }
158165

159166
//[JsonProperty(PropertyName = "test")]

ArkBot/Modules/Application/Configuration/Model/IConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public interface IConfig
1414
ClustersConfigSection Clusters { get; set; }
1515
string PowershellFilePath { get; set; }
1616
bool UseCompatibilityChangeWatcher { get; set; }
17+
Microsoft.Extensions.Logging.LogLevel LogLevel { get; set; }
1718
bool AnonymizeWebApiData { get; set; }
1819

1920
//Test1ConfigSection Test { get; set; }

ArkBot/Modules/Application/ViewModel/Workspace.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,6 @@ static void WriteAndWaitForKey(params string[] msgs)
236236

237237
internal async Task Init()
238238
{
239-
System.Console.WriteLine("ARK Bot");
240-
System.Console.WriteLine("------------------------------------------------------");
241-
System.Console.WriteLine();
242-
243239
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
244240
XmlConfigurator.Configure(logRepository, new FileInfo("App.config")); //"log4net.config"
245241

@@ -578,10 +574,7 @@ internal async Task Init()
578574
{
579575
logging.ClearProviders();
580576
logging.AddDebug();
581-
logging.AddEventLog();
582-
logging.AddEventSourceLogger();
583-
//todo: add log4net logging
584-
//possible to catch exceptions and serialize detailed exception logs?
577+
logging.AddWebApp();
585578
})
586579
.UseUrls(_config.WebApp.Ssl.ChallengeListenPrefix)
587580
.Configure((appBuilder) =>
@@ -690,8 +683,7 @@ internal async Task Init()
690683
{
691684
logging.ClearProviders();
692685
logging.AddDebug();
693-
logging.AddEventLog();
694-
logging.AddEventSourceLogger();
686+
logging.AddWebApp();
695687
})
696688
.UseContentRoot(Path.Combine(AppContext.BaseDirectory, "WebApp")) //Directory.GetCurrentDirectory()
697689
.UseWebRoot(Path.Combine(AppContext.BaseDirectory, "WebApp"))
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.DependencyInjection.Extensions;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace ArkBot.Modules.WebApp
9+
{
10+
public static class WebAppLoggerFactoryExtensions
11+
{
12+
public static ILoggingBuilder AddWebApp(this ILoggingBuilder builder)
13+
{
14+
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, WebAppLoggerProvider>());
15+
16+
return builder;
17+
}
18+
}
19+
20+
[ProviderAlias("WebApp")]
21+
public sealed class WebAppLoggerProvider : ILoggerProvider
22+
{
23+
private readonly Application.Configuration.Model.IConfig _config;
24+
25+
public WebAppLoggerProvider(Application.Configuration.Model.IConfig config)
26+
{
27+
_config = config;
28+
}
29+
30+
public ILogger CreateLogger(string name)
31+
{
32+
return new WebAppLogger(name, _config);
33+
}
34+
35+
public void Dispose() { }
36+
}
37+
38+
public sealed class NullScope : IDisposable
39+
{
40+
public static NullScope Instance { get; } = new NullScope();
41+
42+
private NullScope() { }
43+
44+
public void Dispose() { }
45+
}
46+
47+
internal class WebAppLogger : ILogger
48+
{
49+
private readonly Application.Configuration.Model.IConfig _config;
50+
private readonly string _name;
51+
52+
private static Dictionary<LogLevel, ArkBot.Utils.LogLevel> _logLevels;
53+
54+
static WebAppLogger()
55+
{
56+
_logLevels = new Dictionary<LogLevel, Utils.LogLevel>
57+
{
58+
{ LogLevel.Information, Utils.LogLevel.INFO },
59+
{ LogLevel.Warning, Utils.LogLevel.WARN },
60+
{ LogLevel.Error, Utils.LogLevel.ERROR },
61+
{ LogLevel.Critical, Utils.LogLevel.FATAL },
62+
{ LogLevel.Debug, Utils.LogLevel.DEBUG },
63+
{ LogLevel.Trace, Utils.LogLevel.DEBUG }
64+
};
65+
}
66+
67+
public WebAppLogger(string name, Application.Configuration.Model.IConfig config)
68+
{
69+
_name = name;
70+
_config = config;
71+
}
72+
73+
public IDisposable BeginScope<TState>(TState state)
74+
{
75+
return NullScope.Instance;
76+
}
77+
78+
public bool IsEnabled(LogLevel logLevel)
79+
{
80+
if (logLevel == LogLevel.None) return false;
81+
82+
return logLevel >= (_config?.LogLevel ?? LogLevel.Warning);
83+
}
84+
85+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
86+
{
87+
if (!IsEnabled(logLevel)) return;
88+
89+
if (formatter == null)
90+
{
91+
throw new ArgumentNullException(nameof(formatter));
92+
}
93+
94+
var message = formatter(state, exception);
95+
96+
if (string.IsNullOrEmpty(message))
97+
{
98+
return;
99+
}
100+
101+
if (exception != null)
102+
{
103+
if (!_logLevels.TryGetValue(logLevel, out var internalLogLevel)) internalLogLevel = Utils.LogLevel.INFO;
104+
105+
Application.ViewModel.Workspace.Instance.Console.AddLog(@$"{message} (""{exception.Message}"")");
106+
Utils.Logging.LogException(message, exception, GetType(), internalLogLevel);
107+
}
108+
else
109+
{
110+
Application.ViewModel.Workspace.Instance.Console.AddLog(message);
111+
}
112+
}
113+
}
114+
}

ArkBot/defaultconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,6 @@
167167
"tempFileOutputDirPath": "",
168168
"powershellFilePath": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
169169
"useCompatibilityChangeWatcher": true,
170+
"logLevel": "Warning",
170171
"anonymizeWebApiData": false
171172
}

0 commit comments

Comments
 (0)