1+ using System ;
2+ using System . Linq ;
3+ using System . Collections . Generic ;
4+ using Akka . Event ;
5+ using Serilog . Core ;
6+ using Serilog . Core . Enrichers ;
7+
8+ namespace Akka . Logger . Serilog
9+ {
10+ public class SerilogLoggingAdapter : ILoggingAdapter
11+ {
12+ private readonly ILoggingAdapter adapter ;
13+ private readonly IDictionary < string , ILogEventEnricher > enrichers = new Dictionary < string , ILogEventEnricher > ( ) ;
14+
15+ public SerilogLoggingAdapter ( ILoggingAdapter adapter )
16+ {
17+ this . adapter = adapter ;
18+ }
19+
20+ /// <summary>
21+ /// Check to determine whether the <see cref="F:Akka.Event.LogLevel.DebugLevel" /> is enabled.
22+ /// </summary>
23+ public bool IsDebugEnabled => adapter . IsDebugEnabled ;
24+
25+ /// <summary>
26+ /// Check to determine whether the <see cref="F:Akka.Event.LogLevel.InfoLevel" /> is enabled.
27+ /// </summary>
28+ public bool IsInfoEnabled => adapter . IsInfoEnabled ;
29+
30+ /// <summary>
31+ /// Check to determine whether the <see cref="F:Akka.Event.LogLevel.WarningLevel" /> is enabled.
32+ /// </summary>
33+ public bool IsWarningEnabled => adapter . IsWarningEnabled ;
34+
35+ /// <summary>
36+ /// Check to determine whether the <see cref="F:Akka.Event.LogLevel.ErrorLevel" /> is enabled.
37+ /// </summary>
38+ public bool IsErrorEnabled => adapter . IsErrorEnabled ;
39+
40+ /// <summary>Determines whether a specific log level is enabled.</summary>
41+ /// <param name="logLevel">The log level that is being checked.</param>
42+ /// <returns><c>true</c> if the specified level is enabled; otherwise <c>false</c>.</returns>
43+ public bool IsEnabled ( LogLevel logLevel )
44+ {
45+ return adapter . IsEnabled ( logLevel ) ;
46+ }
47+
48+ /// <summary>
49+ /// Logs a <see cref="F:Akka.Event.LogLevel.DebugLevel" /> message.
50+ /// </summary>
51+ /// <param name="format">The message that is being logged.</param>
52+ /// <param name="args">An optional list of items used to format the message.</param>
53+ public virtual void Debug ( string format , params object [ ] args )
54+ {
55+ adapter . Debug ( format , BuildArgs ( args ) ) ;
56+ }
57+
58+ /// <summary>
59+ /// Logs a <see cref="F:Akka.Event.LogLevel.InfoLevel" /> message.
60+ /// </summary>
61+ /// <param name="format">The message that is being logged.</param>
62+ /// <param name="args">An optional list of items used to format the message.</param>
63+ public virtual void Info ( string format , params object [ ] args )
64+ {
65+ adapter . Info ( format , BuildArgs ( args ) ) ;
66+ }
67+
68+ /// <summary>
69+ /// Obsolete. Use <see cref="M:Akka.Event.ILoggingAdapter.Warning(System.String,System.Object[])" /> instead!
70+ /// </summary>
71+ /// <param name="format">N/A</param>
72+ /// <param name="args">N/A</param>
73+ public virtual void Warn ( string format , params object [ ] args )
74+ {
75+ adapter . Warning ( format , BuildArgs ( args ) ) ;
76+ }
77+
78+ /// <summary>
79+ /// Logs a <see cref="F:Akka.Event.LogLevel.WarningLevel" /> message.
80+ /// </summary>
81+ /// <param name="format">The message that is being logged.</param>
82+ /// <param name="args">An optional list of items used to format the message.</param>
83+ public virtual void Warning ( string format , params object [ ] args )
84+ {
85+ adapter . Warning ( format , BuildArgs ( args ) ) ;
86+ }
87+
88+ /// <summary>
89+ /// Logs a <see cref="F:Akka.Event.LogLevel.ErrorLevel" /> message.
90+ /// </summary>
91+ /// <param name="format">The message that is being logged.</param>
92+ /// <param name="args">An optional list of items used to format the message.</param>
93+ public virtual void Error ( string format , params object [ ] args )
94+ {
95+ adapter . Error ( format , BuildArgs ( args ) ) ;
96+ }
97+
98+ /// <summary>
99+ /// Logs a <see cref="F:Akka.Event.LogLevel.ErrorLevel" /> message and associated exception.
100+ /// </summary>
101+ /// <param name="cause">The exception associated with this message.</param>
102+ /// <param name="format">The message that is being logged.</param>
103+ /// <param name="args">An optional list of items used to format the message.</param>
104+ public virtual void Error ( Exception cause , string format , params object [ ] args )
105+ {
106+ adapter . Error ( cause , format , BuildArgs ( args ) ) ;
107+ }
108+
109+ /// <summary>Logs a message with a specified level.</summary>
110+ /// <param name="logLevel">The level used to log the message.</param>
111+ /// <param name="format">The message that is being logged.</param>
112+ /// <param name="args">An optional list of items used to format the message.</param>
113+ public virtual void Log ( LogLevel logLevel , string format , params object [ ] args )
114+ {
115+ adapter . Log ( logLevel , format , BuildArgs ( args ) ) ;
116+ }
117+
118+ public ILoggingAdapter SetContextProperty ( string name , object value , bool destructureObjects = false )
119+ {
120+ enrichers . Add ( name , new PropertyEnricher ( name , value , destructureObjects ) ) ;
121+ return this ;
122+ }
123+
124+ private object [ ] BuildArgs ( IEnumerable < object > args )
125+ {
126+ var newArgs = args . ToList ( ) ;
127+ newArgs . AddRange ( enrichers . Select ( enricher => enricher . Value ) ) ;
128+ return newArgs . ToArray ( ) ;
129+ }
130+ }
131+ }
0 commit comments