1- using Microsoft . Extensions . Logging ;
2- using System ;
3- using System . Linq ;
4- using System . Runtime . CompilerServices ;
5- using static ArtNetSharp . ApplicationLogging ;
6-
7- [ assembly: InternalsVisibleTo ( "ArtNetTests" ) ]
8- namespace ArtNetSharp
9- {
10- /// <summary>
11- /// Shared logger
12- /// </summary>
13- internal static class ApplicationLogging
14- {
15- private static ILoggerFactory loggerFactory ;
16- internal static ILoggerFactory LoggerFactory
17- {
18- get
19- {
20- if ( loggerFactory == null )
21- {
22- bool isTest = AppDomain . CurrentDomain . GetAssemblies ( )
23- . Any ( a => a . FullName . StartsWith ( "NUnit" , StringComparison . OrdinalIgnoreCase ) ) ;
24- loggerFactory = Microsoft . Extensions . Logging . LoggerFactory . Create ( ( builder ) =>
25- {
26- FileProvider fp = isTest ? new FileProvider ( ) : null ;
27- #if Debug
28- fp ? = new FileProvider ( ) ;
29- #endif
30- if ( isTest )
31- {
32- builder . AddConsole ( ) ;
33- builder . SetMinimumLevel ( LogLevel . Trace ) ;
34- }
35- if ( fp != null )
36- builder . AddProvider ( fp ) ;
37- } ) ;
38- }
39- return loggerFactory ;
40- }
41- }
42-
43- internal static ILogger < T > CreateLogger < T > ( ) => LoggerFactory . CreateLogger < T > ( ) ;
44- internal static ILogger CreateLogger ( Type type ) => LoggerFactory . CreateLogger ( type ) ;
45- internal static ILogger CreateLogger ( string categoryName ) => LoggerFactory . CreateLogger ( categoryName ) ;
46- internal static void LogTrace ( this ILogger logger , Exception exception )
47- {
48- logger . LogTrace ( exception , string . Empty ) ;
49- }
50- internal static void LogDebug ( this ILogger logger , Exception exception )
51- {
52- logger . LogDebug ( exception , string . Empty ) ;
53- }
54- internal static void LogInformation ( this ILogger logger , Exception exception )
55- {
56- logger . LogInformation ( exception , string . Empty ) ;
57- }
58- internal static void LogWarning ( this ILogger logger , Exception exception )
59- {
60- logger . LogWarning ( exception , string . Empty ) ;
61- }
62- internal static void LogError ( this ILogger logger , Exception exception )
63- {
64- logger . LogError ( exception , string . Empty ) ;
65- }
66- internal static void LogCritical ( this ILogger logger , Exception exception )
67- {
68- logger . LogCritical ( exception , string . Empty ) ;
69- }
70- }
1+ using Microsoft . Extensions . Logging ;
2+ using System ;
3+ using System . Linq ;
4+ using System . Runtime . CompilerServices ;
5+ using static ArtNetSharp . Logging ;
6+
7+ [ assembly: InternalsVisibleTo ( "ArtNetTests" ) ]
8+ namespace ArtNetSharp
9+ {
10+ public static class Logging
11+ {
12+ private static ILoggerFactory loggerFactory ;
13+ public static ILoggerFactory LoggerFactory
14+ {
15+ get
16+ {
17+ if ( loggerFactory == null )
18+ {
19+ bool isTest = AppDomain . CurrentDomain . GetAssemblies ( )
20+ . Any ( a => a . FullName . StartsWith ( "NUnit" , StringComparison . OrdinalIgnoreCase ) ) ;
21+ loggerFactory = Microsoft . Extensions . Logging . LoggerFactory . Create ( ( builder ) =>
22+ {
23+ FileProvider fp = isTest ? new FileProvider ( ) : null ;
24+ #if Debug
25+ fp ? = new FileProvider ( ) ;
26+ #endif
27+ if ( isTest )
28+ {
29+ builder . AddConsole ( ) ;
30+ builder . SetMinimumLevel ( LogLevel . Trace ) ;
31+ }
32+ if ( fp != null )
33+ builder . AddProvider ( fp ) ;
34+ } ) ;
35+ }
36+ return loggerFactory ;
37+ }
38+ set
39+ {
40+ if ( loggerFactory != null )
41+ throw new InvalidOperationException ( "LoggerFactory is already set. It can only be set once." ) ;
42+ loggerFactory = value ;
43+ }
44+ }
45+
46+ internal static ILogger < T > CreateLogger < T > ( ) => LoggerFactory . CreateLogger < T > ( ) ;
47+ internal static ILogger CreateLogger ( Type type ) => LoggerFactory . CreateLogger ( type ) ;
48+ internal static ILogger CreateLogger ( string categoryName ) => LoggerFactory . CreateLogger ( categoryName ) ;
49+ internal static void LogTrace ( this ILogger logger , Exception exception )
50+ {
51+ logger . LogTrace ( exception , string . Empty ) ;
52+ }
53+ internal static void LogDebug ( this ILogger logger , Exception exception )
54+ {
55+ logger . LogDebug ( exception , string . Empty ) ;
56+ }
57+ internal static void LogInformation ( this ILogger logger , Exception exception )
58+ {
59+ logger . LogInformation ( exception , string . Empty ) ;
60+ }
61+ internal static void LogWarning ( this ILogger logger , Exception exception )
62+ {
63+ logger . LogWarning ( exception , string . Empty ) ;
64+ }
65+ internal static void LogError ( this ILogger logger , Exception exception )
66+ {
67+ logger . LogError ( exception , string . Empty ) ;
68+ }
69+ internal static void LogCritical ( this ILogger logger , Exception exception )
70+ {
71+ logger . LogCritical ( exception , string . Empty ) ;
72+ }
73+ }
7174}
0 commit comments