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+ }
0 commit comments