23
23
namespace Serilog . Sinks . MSSqlServer
24
24
{
25
25
/// <summary>
26
- /// Writes log events as rows in a table of MSSqlServer database using Audit logic, meaning that each row is synchronously committed
27
- /// and any errors that occur are propagated to the caller.
26
+ /// Writes log events as rows in a table of MSSqlServer database using Audit logic, meaning that each row is synchronously committed
27
+ /// and any errors that occur are propagated to the caller.
28
28
/// </summary>
29
29
public class MSSqlServerAuditSink : ILogEventSink , IDisposable
30
30
{
@@ -83,39 +83,28 @@ internal MSSqlServerAuditSink(
83
83
ColumnOptions columnOptions ,
84
84
SinkDependencies sinkDependencies )
85
85
{
86
- if ( sinkOptions ? . TableName == null )
87
- {
88
- throw new InvalidOperationException ( "Table name must be specified!" ) ;
89
- }
90
-
91
- if ( columnOptions . DisableTriggers )
92
- throw new NotSupportedException ( $ "The { nameof ( ColumnOptions . DisableTriggers ) } option is not supported for auditing.") ;
93
-
94
- if ( sinkDependencies == null )
95
- {
96
- throw new ArgumentNullException ( nameof ( sinkDependencies ) ) ;
97
- }
98
- _sqlLogEventWriter = sinkDependencies ? . SqlLogEventWriter ?? throw new InvalidOperationException ( $ "SqlLogEventWriter is not initialized!") ;
86
+ ValidateParameters ( sinkOptions , columnOptions ) ;
87
+ CheckSinkDependencies ( sinkDependencies ) ;
99
88
100
- if ( sinkOptions . AutoCreateSqlTable )
101
- {
102
- if ( sinkDependencies ? . DataTableCreator == null )
103
- {
104
- throw new InvalidOperationException ( $ "DataTableCreator is not initialized!") ;
105
- }
89
+ _sqlLogEventWriter = sinkDependencies . SqlLogEventWriter ;
106
90
107
- using ( var eventTable = sinkDependencies . DataTableCreator . CreateDataTable ( ) )
108
- {
109
- sinkDependencies . SqlTableCreator . CreateTable ( eventTable ) ;
110
- }
111
- }
91
+ CreateTable ( sinkOptions , sinkDependencies ) ;
112
92
}
113
93
114
94
/// <summary>Emit the provided log event to the sink.</summary>
115
95
/// <param name="logEvent">The log event to write.</param>
116
96
public void Emit ( LogEvent logEvent ) =>
117
97
_sqlLogEventWriter . WriteEvent ( logEvent ) ;
118
98
99
+ /// <summary>
100
+ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
101
+ /// </summary>
102
+ public void Dispose ( )
103
+ {
104
+ Dispose ( true ) ;
105
+ GC . SuppressFinalize ( this ) ;
106
+ }
107
+
119
108
/// <summary>
120
109
/// Releases the unmanaged resources used by the Serilog.Sinks.MSSqlServer.MSSqlServerAuditSink and optionally
121
110
/// releases the managed resources.
@@ -126,13 +115,49 @@ protected virtual void Dispose(bool disposing)
126
115
// This class needn't to dispose anything. This is just here for sink interface compatibility.
127
116
}
128
117
129
- /// <summary>
130
- /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
131
- /// </summary>
132
- public void Dispose ( )
118
+ private static void ValidateParameters ( SinkOptions sinkOptions , ColumnOptions columnOptions )
133
119
{
134
- Dispose ( true ) ;
135
- GC . SuppressFinalize ( this ) ;
120
+ if ( sinkOptions ? . TableName == null )
121
+ {
122
+ throw new InvalidOperationException ( "Table name must be specified!" ) ;
123
+ }
124
+
125
+ if ( columnOptions . DisableTriggers )
126
+ throw new NotSupportedException ( $ "The { nameof ( ColumnOptions . DisableTriggers ) } option is not supported for auditing.") ;
127
+ }
128
+
129
+ private static void CheckSinkDependencies ( SinkDependencies sinkDependencies )
130
+ {
131
+ if ( sinkDependencies == null )
132
+ {
133
+ throw new ArgumentNullException ( nameof ( sinkDependencies ) ) ;
134
+ }
135
+
136
+ if ( sinkDependencies . DataTableCreator == null )
137
+ {
138
+ throw new InvalidOperationException ( $ "DataTableCreator is not initialized!") ;
139
+ }
140
+
141
+ if ( sinkDependencies . SqlTableCreator == null )
142
+ {
143
+ throw new InvalidOperationException ( $ "SqlTableCreator is not initialized!") ;
144
+ }
145
+
146
+ if ( sinkDependencies . SqlLogEventWriter == null )
147
+ {
148
+ throw new InvalidOperationException ( $ "SqlLogEventWriter is not initialized!") ;
149
+ }
150
+ }
151
+
152
+ private static void CreateTable ( SinkOptions sinkOptions , SinkDependencies sinkDependencies )
153
+ {
154
+ if ( sinkOptions . AutoCreateSqlTable )
155
+ {
156
+ using ( var eventTable = sinkDependencies . DataTableCreator . CreateDataTable ( ) )
157
+ {
158
+ sinkDependencies . SqlTableCreator . CreateTable ( eventTable ) ;
159
+ }
160
+ }
136
161
}
137
162
}
138
163
}
0 commit comments