3
3
//
4
4
// https://github.com/damianh/LibLog
5
5
//===============================================================================
6
- // Copyright © 2011-2015 Damian Hickey. All rights reserved.
6
+ // Copyright  © 2011-2015 Damian Hickey. All rights reserved.
7
7
//
8
8
// Permission is hereby granted, free of charge, to any person obtaining a copy
9
9
// of this software and associated documentation files (the "Software"), to deal
29
29
// Define LIBLOG_PORTABLE conditional compilation symbol for PCL compatibility
30
30
//
31
31
// Define LIBLOG_PUBLIC to enable ability to GET a logger (LogProvider.For<>() etc) from outside this library. NOTE:
32
- // this can have unintendend consequences of consumers of your library using your library to resolve a logger. If the
32
+ // this can have unintended consequences of consumers of your library using your library to resolve a logger. If the
33
33
// reason is because you want to open this functionality to other projects within your solution,
34
34
// consider [InternalVisibleTo] instead.
35
35
//
38
38
39
39
#pragma warning disable 1591
40
40
41
- #define LIBLOG_PUBLIC
42
-
43
41
// If you copied this file manually, you need to change all "YourRootNameSpace" so not to clash with other libraries
44
42
// that use LibLog
45
43
#if LIBLOG_PROVIDERS_ONLY
@@ -57,8 +55,10 @@ namespace GitTools.Logging
57
55
using System ;
58
56
#if ! LIBLOG_PROVIDERS_ONLY
59
57
using System . Diagnostics ;
58
+ #if ! LIBLOG_PORTABLE
60
59
using System . Runtime . CompilerServices ;
61
60
#endif
61
+ #endif
62
62
63
63
#if LIBLOG_PROVIDERS_ONLY
64
64
internal
@@ -198,6 +198,7 @@ public static void DebugException(this ILog logger, string message, Exception ex
198
198
199
199
public static void Error ( this ILog logger , Func < string > messageFunc )
200
200
{
201
+ GuardAgainstNullLogger ( logger ) ;
201
202
logger . Log ( LogLevel . Error , messageFunc ) ;
202
203
}
203
204
@@ -422,8 +423,8 @@ static class LogProvider
422
423
public const string DisableLoggingEnvironmentVariable = "GitTools_LIBLOG_DISABLE" ;
423
424
private const string NullLogProvider = "Current Log Provider is not set. Call SetCurrentLogProvider " +
424
425
"with a non-null value first." ;
425
- private static dynamic _currentLogProvider ;
426
- private static Action < ILogProvider > _onCurrentLogProviderSet ;
426
+ private static dynamic s_currentLogProvider ;
427
+ private static Action < ILogProvider > s_onCurrentLogProviderSet ;
427
428
428
429
static LogProvider ( )
429
430
{
@@ -436,7 +437,7 @@ static LogProvider()
436
437
/// <param name="logProvider">The log provider.</param>
437
438
public static void SetCurrentLogProvider ( ILogProvider logProvider )
438
439
{
439
- _currentLogProvider = logProvider ;
440
+ s_currentLogProvider = logProvider ;
440
441
441
442
RaiseOnCurrentLogProviderSet ( ) ;
442
443
}
@@ -459,7 +460,7 @@ internal static Action<ILogProvider> OnCurrentLogProviderSet
459
460
{
460
461
set
461
462
{
462
- _onCurrentLogProviderSet = value ;
463
+ s_onCurrentLogProviderSet = value ;
463
464
RaiseOnCurrentLogProviderSet ( ) ;
464
465
}
465
466
}
@@ -468,7 +469,7 @@ internal static ILogProvider CurrentLogProvider
468
469
{
469
470
get
470
471
{
471
- return _currentLogProvider ;
472
+ return s_currentLogProvider ;
472
473
}
473
474
}
474
475
@@ -610,9 +611,9 @@ static IDisposable OpenMappedContext(string key, string value)
610
611
#if ! LIBLOG_PROVIDERS_ONLY
611
612
private static void RaiseOnCurrentLogProviderSet ( )
612
613
{
613
- if ( _onCurrentLogProviderSet != null )
614
+ if ( s_onCurrentLogProviderSet != null )
614
615
{
615
- _onCurrentLogProviderSet ( _currentLogProvider ) ;
616
+ s_onCurrentLogProviderSet ( s_currentLogProvider ) ;
616
617
}
617
618
}
618
619
#endif
@@ -636,7 +637,7 @@ internal static ILogProvider ResolveLogProvider()
636
637
#else
637
638
Console . WriteLine (
638
639
#endif
639
- "Exception occured resolving a log provider. Logging for this assembly {0} is disabled. {1}" ,
640
+ "Exception occurred resolving a log provider. Logging for this assembly {0} is disabled. {1}" ,
640
641
typeof ( LogProvider ) . GetAssemblyPortable ( ) . FullName ,
641
642
ex ) ;
642
643
}
@@ -676,15 +677,14 @@ internal Logger WrappedLogger
676
677
677
678
public bool Log ( LogLevel logLevel , Func < string > messageFunc , Exception exception = null , params object [ ] formatParameters )
678
679
{
679
- #if LIBLOG_PORTABLE
680
680
if ( _getIsDisabled ( ) )
681
681
{
682
682
return false ;
683
683
}
684
- #else
684
+ #if ! LIBLOG_PORTABLE
685
685
var envVar = Environment . GetEnvironmentVariable ( LogProvider . DisableLoggingEnvironmentVariable ) ;
686
686
687
- if ( _getIsDisabled ( ) || ( envVar != null && envVar . Equals ( "true" , StringComparison . OrdinalIgnoreCase ) ) )
687
+ if ( envVar != null && envVar . Equals ( "true" , StringComparison . OrdinalIgnoreCase ) )
688
688
{
689
689
return false ;
690
690
}
@@ -721,12 +721,16 @@ namespace GitTools.Logging.LogProviders
721
721
{
722
722
using System ;
723
723
using System . Collections . Generic ;
724
+ #if ! LIBLOG_PORTABLE
724
725
using System . Diagnostics ;
726
+ #endif
725
727
using System . Globalization ;
726
728
using System . Linq ;
727
729
using System . Linq . Expressions ;
728
730
using System . Reflection ;
731
+ #if ! LIBLOG_PORTABLE
729
732
using System . Text ;
733
+ #endif
730
734
using System . Text . RegularExpressions ;
731
735
732
736
internal abstract class LogProviderBase : ILogProvider
@@ -772,7 +776,7 @@ protected virtual OpenMdc GetOpenMdcMethod()
772
776
internal class NLogLogProvider : LogProviderBase
773
777
{
774
778
private readonly Func < string , object > _getLoggerByNameDelegate ;
775
- private static bool _providerIsAvailableOverride = true ;
779
+ private static bool s_providerIsAvailableOverride = true ;
776
780
777
781
public NLogLogProvider ( )
778
782
{
@@ -785,8 +789,8 @@ public NLogLogProvider()
785
789
786
790
public static bool ProviderIsAvailableOverride
787
791
{
788
- get { return _providerIsAvailableOverride ; }
789
- set { _providerIsAvailableOverride = value ; }
792
+ get { return s_providerIsAvailableOverride ; }
793
+ set { s_providerIsAvailableOverride = value ; }
790
794
}
791
795
792
796
public override Logger GetLogger ( string name )
@@ -991,7 +995,7 @@ private bool IsLogLevelEnable(LogLevel logLevel)
991
995
internal class Log4NetLogProvider : LogProviderBase
992
996
{
993
997
private readonly Func < string , object > _getLoggerByNameDelegate ;
994
- private static bool _providerIsAvailableOverride = true ;
998
+ private static bool s_providerIsAvailableOverride = true ;
995
999
996
1000
public Log4NetLogProvider ( )
997
1001
{
@@ -1004,8 +1008,8 @@ public Log4NetLogProvider()
1004
1008
1005
1009
public static bool ProviderIsAvailableOverride
1006
1010
{
1007
- get { return _providerIsAvailableOverride ; }
1008
- set { _providerIsAvailableOverride = value ; }
1011
+ get { return s_providerIsAvailableOverride ; }
1012
+ set { s_providerIsAvailableOverride = value ; }
1009
1013
}
1010
1014
1011
1015
public override Logger GetLogger ( string name )
@@ -1070,15 +1074,16 @@ private static Func<string, object> GetGetLoggerMethodCall()
1070
1074
internal class Log4NetLogger
1071
1075
{
1072
1076
private readonly dynamic _logger ;
1073
- private static Type _callerStackBoundaryType ;
1077
+ private static Type s_callerStackBoundaryType ;
1078
+ private static readonly object CallerStackBoundaryTypeSync = new object ( ) ;
1074
1079
1075
1080
private readonly object _levelDebug ;
1076
1081
private readonly object _levelInfo ;
1077
1082
private readonly object _levelWarn ;
1078
1083
private readonly object _levelError ;
1079
1084
private readonly object _levelFatal ;
1080
- private readonly Func < object , object , bool > isEnabledForDelegate ;
1081
- private Action < object , Type , object , string , Exception > logDelegate ;
1085
+ private readonly Func < object , object , bool > _isEnabledForDelegate ;
1086
+ private readonly Action < object , Type , object , string , Exception > _logDelegate ;
1082
1087
1083
1088
internal Log4NetLogger ( dynamic logger )
1084
1089
{
@@ -1111,7 +1116,7 @@ internal Log4NetLogger(dynamic logger)
1111
1116
ParameterExpression messageParam = Expression . Parameter ( typeof ( string ) ) ;
1112
1117
UnaryExpression levelCast = Expression . Convert ( levelParam , logEventLevelType ) ;
1113
1118
MethodCallExpression isEnabledMethodCall = Expression . Call ( instanceCast , isEnabledMethodInfo , levelCast ) ;
1114
- isEnabledForDelegate = Expression . Lambda < Func < object , object , bool > > ( isEnabledMethodCall , instanceParam , levelParam ) . Compile ( ) ;
1119
+ _isEnabledForDelegate = Expression . Lambda < Func < object , object , bool > > ( isEnabledMethodCall , instanceParam , levelParam ) . Compile ( ) ;
1115
1120
1116
1121
// Action<object, object, string, Exception> Log =
1117
1122
// (logger, callerStackBoundaryDeclaringType, level, message, exception) => { ((ILogger)logger).Write(callerStackBoundaryDeclaringType, level, message, exception); }
@@ -1128,7 +1133,7 @@ internal Log4NetLogger(dynamic logger)
1128
1133
levelCast ,
1129
1134
messageParam ,
1130
1135
exceptionParam ) ;
1131
- logDelegate = Expression . Lambda < Action < object , Type , object , string , Exception > > (
1136
+ _logDelegate = Expression . Lambda < Action < object , Type , object , string , Exception > > (
1132
1137
writeMethodExp ,
1133
1138
instanceParam ,
1134
1139
callerStackBoundaryDeclaringTypeParam ,
@@ -1144,38 +1149,38 @@ public bool Log(LogLevel logLevel, Func<string> messageFunc, Exception exception
1144
1149
return IsLogLevelEnable ( logLevel ) ;
1145
1150
}
1146
1151
1147
- messageFunc = LogMessageFormatter . SimulateStructuredLogging ( messageFunc , formatParameters ) ;
1148
-
1149
1152
if ( ! IsLogLevelEnable ( logLevel ) )
1150
1153
{
1151
1154
return false ;
1152
1155
}
1153
1156
1157
+ messageFunc = LogMessageFormatter . SimulateStructuredLogging ( messageFunc , formatParameters ) ;
1158
+
1154
1159
// determine correct caller - this might change due to jit optimizations with method inlining
1155
- if ( _callerStackBoundaryType == null )
1160
+ if ( s_callerStackBoundaryType == null )
1156
1161
{
1157
- lock ( GetType ( ) )
1162
+ lock ( CallerStackBoundaryTypeSync )
1158
1163
{
1159
1164
#if ! LIBLOG_PORTABLE
1160
1165
StackTrace stack = new StackTrace ( ) ;
1161
1166
Type thisType = GetType ( ) ;
1162
- _callerStackBoundaryType = Type . GetType ( "LoggerExecutionWrapper" ) ;
1163
- for ( int i = 1 ; i < stack . FrameCount ; i ++ )
1167
+ s_callerStackBoundaryType = Type . GetType ( "LoggerExecutionWrapper" ) ;
1168
+ for ( var i = 1 ; i < stack . FrameCount ; i ++ )
1164
1169
{
1165
1170
if ( ! IsInTypeHierarchy ( thisType , stack . GetFrame ( i ) . GetMethod ( ) . DeclaringType ) )
1166
1171
{
1167
- _callerStackBoundaryType = stack . GetFrame ( i - 1 ) . GetMethod ( ) . DeclaringType ;
1172
+ s_callerStackBoundaryType = stack . GetFrame ( i - 1 ) . GetMethod ( ) . DeclaringType ;
1168
1173
break ;
1169
1174
}
1170
1175
}
1171
1176
#else
1172
- _callerStackBoundaryType = typeof ( LoggerExecutionWrapper ) ;
1177
+ s_callerStackBoundaryType = typeof ( LoggerExecutionWrapper ) ;
1173
1178
#endif
1174
1179
}
1175
1180
}
1176
1181
1177
1182
var translatedLevel = TranslateLevel ( logLevel ) ;
1178
- logDelegate ( _logger , _callerStackBoundaryType , translatedLevel , messageFunc ( ) , exception ) ;
1183
+ _logDelegate ( _logger , s_callerStackBoundaryType , translatedLevel , messageFunc ( ) , exception ) ;
1179
1184
return true ;
1180
1185
}
1181
1186
@@ -1195,7 +1200,7 @@ private bool IsInTypeHierarchy(Type currentType, Type checkType)
1195
1200
private bool IsLogLevelEnable ( LogLevel logLevel )
1196
1201
{
1197
1202
var level = TranslateLevel ( logLevel ) ;
1198
- return isEnabledForDelegate ( _logger , level ) ;
1203
+ return _isEnabledForDelegate ( _logger , level ) ;
1199
1204
}
1200
1205
1201
1206
private object TranslateLevel ( LogLevel logLevel )
@@ -1223,7 +1228,7 @@ private object TranslateLevel(LogLevel logLevel)
1223
1228
internal class EntLibLogProvider : LogProviderBase
1224
1229
{
1225
1230
private const string TypeTemplate = "Microsoft.Practices.EnterpriseLibrary.Logging.{0}, Microsoft.Practices.EnterpriseLibrary.Logging" ;
1226
- private static bool _providerIsAvailableOverride = true ;
1231
+ private static bool s_providerIsAvailableOverride = true ;
1227
1232
private static readonly Type LogEntryType ;
1228
1233
private static readonly Type LoggerType ;
1229
1234
private static readonly Type TraceEventTypeType ;
@@ -1255,8 +1260,8 @@ public EntLibLogProvider()
1255
1260
1256
1261
public static bool ProviderIsAvailableOverride
1257
1262
{
1258
- get { return _providerIsAvailableOverride ; }
1259
- set { _providerIsAvailableOverride = value ; }
1263
+ get { return s_providerIsAvailableOverride ; }
1264
+ set { s_providerIsAvailableOverride = value ; }
1260
1265
}
1261
1266
1262
1267
public override Logger GetLogger ( string name )
@@ -1395,7 +1400,7 @@ private static int MapSeverity(LogLevel logLevel)
1395
1400
internal class SerilogLogProvider : LogProviderBase
1396
1401
{
1397
1402
private readonly Func < string , object > _getLoggerByNameDelegate ;
1398
- private static bool _providerIsAvailableOverride = true ;
1403
+ private static bool s_providerIsAvailableOverride = true ;
1399
1404
1400
1405
public SerilogLogProvider ( )
1401
1406
{
@@ -1408,8 +1413,8 @@ public SerilogLogProvider()
1408
1413
1409
1414
public static bool ProviderIsAvailableOverride
1410
1415
{
1411
- get { return _providerIsAvailableOverride ; }
1412
- set { _providerIsAvailableOverride = value ; }
1416
+ get { return s_providerIsAvailableOverride ; }
1417
+ set { s_providerIsAvailableOverride = value ; }
1413
1418
}
1414
1419
1415
1420
public override Logger GetLogger ( string name )
@@ -1702,7 +1707,7 @@ internal delegate void WriteDelegate(
1702
1707
params object [ ] args
1703
1708
) ;
1704
1709
1705
- private static bool _providerIsAvailableOverride = true ;
1710
+ private static bool s_providerIsAvailableOverride = true ;
1706
1711
private readonly WriteDelegate _logWriteDelegate ;
1707
1712
1708
1713
public LoupeLogProvider ( )
@@ -1723,8 +1728,8 @@ public LoupeLogProvider()
1723
1728
/// </value>
1724
1729
public static bool ProviderIsAvailableOverride
1725
1730
{
1726
- get { return _providerIsAvailableOverride ; }
1727
- set { _providerIsAvailableOverride = value ; }
1731
+ get { return s_providerIsAvailableOverride ; }
1732
+ set { s_providerIsAvailableOverride = value ; }
1728
1733
}
1729
1734
1730
1735
public override Logger GetLogger ( string name )
@@ -1848,7 +1853,7 @@ internal static class LogMessageFormatter
1848
1853
/// <summary>
1849
1854
/// Some logging frameworks support structured logging, such as serilog. This will allow you to add names to structured data in a format string:
1850
1855
/// For example: Log("Log message to {user}", user). This only works with serilog, but as the user of LibLog, you don't know if serilog is actually
1851
- /// used. So, this class simulates that. it will replace any text in {curlybraces } with an index number.
1856
+ /// used. So, this class simulates that. it will replace any text in {curly braces } with an index number.
1852
1857
///
1853
1858
/// "Log {message} to {user}" would turn into => "Log {0} to {1}". Then the format parameters are handled using regular .net string.Format.
1854
1859
/// </summary>
0 commit comments