77
88namespace Akka . Logger . Serilog
99{
10- public class SerilogLoggingAdapter : BusLogging
10+ internal readonly struct SerilogPayload
11+ {
12+ public SerilogPayload ( object message , IReadOnlyList < ILogEventEnricher > enrichers )
13+ {
14+ Message = message ;
15+ Enrichers = enrichers ;
16+ }
17+
18+ public IReadOnlyList < ILogEventEnricher > Enrichers { get ; }
19+
20+ public object Message { get ; }
21+
22+ public override string ToString ( )
23+ {
24+ return Message . ToString ( ) ;
25+ }
26+ }
27+
28+ public class SerilogLoggingAdapter : LoggingAdapterBase
1129 {
1230 private readonly LoggingBus _bus ;
1331 private readonly Type _logClass ;
@@ -29,98 +47,36 @@ public SerilogLoggingAdapter(LoggingBus bus, string logSource, Type logClass) :
2947 {
3048 }
3149
32- private SerilogLoggingAdapter ( LoggingBus bus , string logSource , Type logClass , ContextNode enricher ) : base ( bus , logSource , logClass , SerilogLogMessageFormatter . Instance )
50+ private SerilogLoggingAdapter ( LoggingBus bus , string logSource , Type logClass , ContextNode enricher ) : base ( SerilogLogMessageFormatter . Instance )
3351 {
3452 _bus = bus ;
3553 _logSource = logSource ;
3654 _logClass = logClass ;
3755 _enricherNode = enricher ;
56+
57+ IsErrorEnabled = bus . LogLevel <= LogLevel . ErrorLevel ;
58+ IsWarningEnabled = bus . LogLevel <= LogLevel . WarningLevel ;
59+ IsInfoEnabled = bus . LogLevel <= LogLevel . InfoLevel ;
60+ IsDebugEnabled = bus . LogLevel <= LogLevel . DebugLevel ;
3861 }
62+
63+ private LogEvent CreateLogEvent ( LogLevel logLevel , object message , Exception cause = null )
64+ => logLevel switch
65+ {
66+ LogLevel . DebugLevel => new Debug ( cause , _logSource , _logClass , BuildMessage ( message ) ) ,
67+ LogLevel . InfoLevel => new Info ( cause , _logSource , _logClass , BuildMessage ( message ) ) ,
68+ LogLevel . WarningLevel => new Warning ( cause , _logSource , _logClass , BuildMessage ( message ) ) ,
69+ LogLevel . ErrorLevel => new Error ( cause , _logSource , _logClass , BuildMessage ( message ) ) ,
70+ _ => throw new ArgumentOutOfRangeException ( nameof ( logLevel ) , logLevel , null )
71+ } ;
3972
40- /// <summary>
41- /// Logs a <see cref="F:Akka.Event.LogLevel.DebugLevel" /> message.
42- /// </summary>
43- /// <param name="format">The message that is being logged.</param>
44- /// <param name="args">An optional list of items used to format the message.</param>
45- public override void Debug ( string format , params object [ ] args )
46- {
47- base . Debug ( format , BuildArgs ( args ) ) ;
48- }
49-
50- /// <summary>
51- /// Logs a <see cref="F:Akka.Event.LogLevel.InfoLevel" /> message.
52- /// </summary>
53- /// <param name="format">The message that is being logged.</param>
54- /// <param name="args">An optional list of items used to format the message.</param>
55- public override void Info ( string format , params object [ ] args )
56- {
57- base . Info ( format , BuildArgs ( args ) ) ;
58- }
59-
60- public override void Info ( Exception cause , string format , params object [ ] args )
61- {
62- base . Info ( cause , format , BuildArgs ( args ) ) ;
63- }
64-
65- public override void Debug ( Exception cause , string format , params object [ ] args )
66- {
67- base . Debug ( cause , format , BuildArgs ( args ) ) ;
68- }
69-
70- /// <summary>
71- /// Obsolete. Use <see cref="M:Akka.Event.ILoggingAdapter.Warning(System.String,System.Object[])" /> instead!
72- /// </summary>
73- /// <param name="format">The message that is being logged.</param>
74- /// <param name="args">An optional list of items used to format the message.</param>
75- public override void Warn ( string format , params object [ ] args )
76- {
77- base . Warning ( format , BuildArgs ( args ) ) ;
78- }
79-
80- public override void Warning ( Exception cause , string format , params object [ ] args )
81- {
82- base . Warning ( cause , format , BuildArgs ( args ) ) ;
83- }
84-
85- /// <summary>
86- /// Logs a <see cref="F:Akka.Event.LogLevel.WarningLevel" /> message.
87- /// </summary>
88- /// <param name="format">The message that is being logged.</param>
89- /// <param name="args">An optional list of items used to format the message.</param>
90- public override void Warning ( string format , params object [ ] args )
91- {
92- base . Warning ( format , BuildArgs ( args ) ) ;
93- }
94-
95- /// <summary>
96- /// Logs a <see cref="F:Akka.Event.LogLevel.ErrorLevel" /> message.
97- /// </summary>
98- /// <param name="format">The message that is being logged.</param>
99- /// <param name="args">An optional list of items used to format the message.</param>
100- public override void Error ( string format , params object [ ] args )
101- {
102- base . Error ( format , BuildArgs ( args ) ) ;
103- }
104-
105- /// <summary>
106- /// Logs a <see cref="F:Akka.Event.LogLevel.ErrorLevel" /> message and associated exception.
107- /// </summary>
108- /// <param name="cause">The exception associated with this message.</param>
109- /// <param name="format">The message that is being logged.</param>
110- /// <param name="args">An optional list of items used to format the message.</param>
111- public override void Error ( Exception cause , string format , params object [ ] args )
112- {
113- base . Error ( cause , format , BuildArgs ( args ) ) ;
114- }
73+ protected override void NotifyLog ( LogLevel logLevel , object message , Exception cause = null )
74+ => _bus . Publish ( CreateLogEvent ( logLevel , message , cause ) ) ;
11575
116- /// <summary>Logs a message with a specified level.</summary>
117- /// <param name="logLevel">The level used to log the message.</param>
118- /// <param name="format">The message that is being logged.</param>
119- /// <param name="args">An optional list of items used to format the message.</param>
120- public override void Log ( LogLevel logLevel , string format , params object [ ] args )
121- {
122- base . Log ( logLevel , format , BuildArgs ( args ) ) ;
123- }
76+ public override bool IsDebugEnabled { get ; }
77+ public override bool IsInfoEnabled { get ; }
78+ public override bool IsWarningEnabled { get ; }
79+ public override bool IsErrorEnabled { get ; }
12480
12581 public ILoggingAdapter SetContextProperty ( string name , object value , bool destructureObjects = false )
12682 {
@@ -134,20 +90,26 @@ public ILoggingAdapter SetContextProperty(string name, object value, bool destru
13490
13591 return new SerilogLoggingAdapter ( _bus , _logSource , _logClass , contextNode ) ;
13692 }
93+
94+ private object BuildMessage ( object message )
95+ {
96+ return new SerilogPayload ( message , BuildArgs ( ) ) ;
97+ }
13798
138- private object [ ] BuildArgs ( IEnumerable < object > args )
99+ private IReadOnlyList < ILogEventEnricher > BuildArgs ( )
139100 {
140- var newArgs = args . ToList ( ) ;
141- if ( _enricherNode != null )
101+ if ( _enricherNode == null )
102+ return Array . Empty < ILogEventEnricher > ( ) ;
103+
104+ var newArgs = new List < ILogEventEnricher > ( ) ;
105+ var currentNode = _enricherNode ;
106+ while ( currentNode != null )
142107 {
143- var currentNode = _enricherNode ;
144- while ( currentNode != null )
145- {
146- newArgs . Add ( currentNode . Enricher ) ;
147- currentNode = currentNode . Next ;
148- }
108+ newArgs . Add ( currentNode . Enricher ) ;
109+ currentNode = currentNode . Next ;
149110 }
150- return newArgs . ToArray ( ) ;
111+
112+ return newArgs ;
151113 }
152114 }
153115}
0 commit comments