1+ namespace GitVersion
2+ {
3+ using System ;
4+ using GitTools . Logging ;
5+
6+ /// <summary>
7+ /// Wraps the <see cref="Logger" /> for use by GitTools.
8+ /// </summary>
9+ public class LoggerWrapper : ILogProvider
10+ {
11+ public GitTools . Logging . Logger GetLogger ( string name )
12+ {
13+ return Log ;
14+ }
15+
16+ public IDisposable OpenNestedContext ( string message )
17+ {
18+ throw new NotImplementedException ( ) ;
19+ }
20+
21+ public IDisposable OpenMappedContext ( string key , string value )
22+ {
23+ throw new NotImplementedException ( ) ;
24+ }
25+
26+ private static bool Log ( LogLevel loglevel , Func < string > messagefunc , Exception exception , object [ ] formatparameters )
27+ {
28+ // Create the main message. Careful of string format errors.
29+ string message ;
30+ if ( messagefunc == null )
31+ {
32+ message = null ;
33+ }
34+ else
35+ {
36+ if ( formatparameters == null || formatparameters . Length == 0 )
37+ {
38+ message = messagefunc ( ) ;
39+ }
40+ else
41+ {
42+ try
43+ {
44+ message = string . Format ( messagefunc ( ) , formatparameters ) ;
45+ }
46+ catch ( FormatException )
47+ {
48+ message = messagefunc ( ) ;
49+ Logger . WriteError ( string . Format ( "LoggerWrapper.Log(): Incorrectly formatted string: message: '{0}'; formatparameters: {1}" , message , string . Join ( ";" , formatparameters ) ) ) ;
50+ }
51+ }
52+ }
53+
54+ if ( exception != null )
55+ {
56+ // Append the exception to the end of the message.
57+ message = string . IsNullOrEmpty ( message ) ? exception . ToString ( ) : string . Format ( "{0}\n {1}" , message , exception ) ;
58+ }
59+
60+ if ( ! string . IsNullOrEmpty ( message ) )
61+ {
62+ switch ( loglevel )
63+ {
64+ case LogLevel . Trace :
65+ case LogLevel . Debug :
66+ Logger . WriteDebug ( message ) ;
67+ break ;
68+ case LogLevel . Info :
69+ Logger . WriteInfo ( message ) ;
70+ break ;
71+ case LogLevel . Warn :
72+ Logger . WriteWarning ( message ) ;
73+ break ;
74+ case LogLevel . Error :
75+ case LogLevel . Fatal :
76+ Logger . WriteError ( message ) ;
77+ break ;
78+ }
79+ }
80+
81+ return true ;
82+ }
83+ }
84+ }
0 commit comments