1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . IO ;
4
+ using System . Text ;
5
+ using System . Xml ;
6
+ using AwesomeAssertions ;
7
+ using Microsoft . Extensions . Configuration ;
8
+ using ReflectionMagic ;
9
+ using Serilog . Core ;
10
+ using Serilog . Settings . Configuration ;
11
+ using Serilog . Sinks . File ;
12
+ using Xunit ;
13
+
14
+ namespace Serilog . Formatting . Log4Net . Tests ;
15
+
16
+ public class SerilogSettingsConfigurationTest
17
+ {
18
+ [ Fact ]
19
+ public void ConfigureDefault ( )
20
+ {
21
+ // lang=json
22
+ var config =
23
+ """
24
+ {
25
+ "Serilog": {
26
+ "WriteTo:File": {
27
+ "Name": "File",
28
+ "Args": {
29
+ "path": "logs.xml",
30
+ "formatter": {
31
+ "type": "Serilog.Formatting.Log4Net.Log4NetTextFormatter, Serilog.Formatting.Log4Net"
32
+ }
33
+ }
34
+ }
35
+ }
36
+ }
37
+ """ ;
38
+
39
+ var options = GetLog4NetTextFormatterOptions ( config ) ;
40
+ IFormatProvider ? formatProvider = GetFormatProvider ( options ) ;
41
+ CDataMode cDataMode = GetCDataMode ( options ) ;
42
+ string nullText = GetNullText ( options ) ;
43
+ XmlQualifiedName ? xmlNamespace = GetXmlNamespace ( options ) ;
44
+ XmlWriterSettings xmlWriterSettings = GetXmlWriterSettings ( options ) ;
45
+ PropertyFilter filterProperty = GetFilterProperty ( options ) ;
46
+ MessageFormatter formatMessage = GetFormatMessage ( options ) ;
47
+ ExceptionFormatter formatException = GetFormatException ( options ) ;
48
+
49
+ formatProvider . Should ( ) . BeNull ( ) ;
50
+ cDataMode . Should ( ) . Be ( CDataMode . Always ) ;
51
+ nullText . Should ( ) . Be ( "(null)" ) ;
52
+ xmlNamespace . Should ( ) . BeEquivalentTo ( new XmlQualifiedName ( "log4net" , "http://logging.apache.org/log4net/schemas/log4net-events-1.2/" ) ) ;
53
+ }
54
+
55
+ [ Theory ]
56
+ [ InlineData ( CDataMode . Always ) ]
57
+ [ InlineData ( CDataMode . Never ) ]
58
+ [ InlineData ( CDataMode . IfNeeded ) ]
59
+ public void ConfigureCDataMode ( CDataMode inputCDataMode )
60
+ {
61
+ // lang=json
62
+ var config =
63
+ $$ """
64
+ {
65
+ "Serilog": {
66
+ "WriteTo:File": {
67
+ "Name": "File",
68
+ "Args": {
69
+ "path": "logs.xml",
70
+ "formatter": {
71
+ "type": "Serilog.Formatting.Log4Net.Log4NetTextFormatter, Serilog.Formatting.Log4Net",
72
+ "cDataMode": "{{ inputCDataMode }} "
73
+ }
74
+ }
75
+ }
76
+ }
77
+ }
78
+ """ ;
79
+
80
+ CDataMode cDataMode = GetCDataMode ( GetLog4NetTextFormatterOptions ( config ) ) ;
81
+
82
+ cDataMode . Should ( ) . Be ( inputCDataMode ) ;
83
+ }
84
+
85
+ [ Theory ]
86
+ [ InlineData ( "" ) ]
87
+ [ InlineData ( "<null>" ) ]
88
+ [ InlineData ( "🌀" ) ]
89
+ public void ConfigureNullText ( string inputNullText )
90
+ {
91
+ // lang=json
92
+ var config =
93
+ $$ """
94
+ {
95
+ "Serilog": {
96
+ "WriteTo:File": {
97
+ "Name": "File",
98
+ "Args": {
99
+ "path": "logs.xml",
100
+ "formatter": {
101
+ "type": "Serilog.Formatting.Log4Net.Log4NetTextFormatter, Serilog.Formatting.Log4Net",
102
+ "nullText": "{{ inputNullText }} "
103
+ }
104
+ }
105
+ }
106
+ }
107
+ }
108
+ """ ;
109
+
110
+ string nullText = GetNullText ( GetLog4NetTextFormatterOptions ( config ) ) ;
111
+
112
+ nullText . Should ( ) . Be ( inputNullText ) ;
113
+ }
114
+
115
+ private static IFormatProvider ? GetFormatProvider ( dynamic options ) => options . FormatProvider ;
116
+ private static CDataMode GetCDataMode ( dynamic options ) => options . CDataMode ;
117
+ private static string GetNullText ( dynamic options ) => options . NullText ;
118
+ private static XmlQualifiedName ? GetXmlNamespace ( dynamic options ) => options . XmlNamespace ;
119
+ private static XmlWriterSettings GetXmlWriterSettings ( dynamic options ) => options . XmlWriterSettings ;
120
+ private static PropertyFilter GetFilterProperty ( dynamic options ) => options . FilterProperty ;
121
+ private static MessageFormatter GetFormatMessage ( dynamic options ) => options . FormatMessage ;
122
+ private static ExceptionFormatter GetFormatException ( dynamic options ) => options . FormatException ;
123
+
124
+ private static dynamic GetLog4NetTextFormatterOptions ( string config )
125
+ {
126
+ using var configStream = new MemoryStream ( Encoding . UTF8 . GetBytes ( config ) ) ;
127
+ var configuration = new ConfigurationBuilder ( ) . AddJsonStream ( configStream ) . Build ( ) ;
128
+ var options = new ConfigurationReaderOptions ( typeof ( ILogger ) . Assembly , typeof ( FileSink ) . Assembly , typeof ( Log4NetTextFormatter ) . Assembly ) ;
129
+ using var logger = new LoggerConfiguration ( ) . ReadFrom . Configuration ( configuration , options ) . CreateLogger ( ) ;
130
+ IEnumerable < ILogEventSink > sinks = logger . AsDynamic ( ) . _sink . _sinks ;
131
+ var fileSink = sinks . Should ( ) . ContainSingle ( ) . Which . Should ( ) . BeOfType < FileSink > ( ) . Subject ;
132
+ return fileSink . AsDynamic ( ) . _textFormatter . _options ;
133
+ }
134
+ }
0 commit comments