forked from serilog-mssql/serilog-sinks-mssqlserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Updated reference to Serilog version 2.5.0 as suggested in serilog-mssql#110 * Added a new Sink `MSSqlServerAuditSink` that persists LogEvents to the database immediately and propagates any errors that occur. * Moved, and refactored common functionality from `MSSqlServerSink` into `MSSqlServerSinkTraits` which is now used by both sinks.
- Loading branch information
1 parent
a6fc78e
commit 9edb6e2
Showing
10 changed files
with
797 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
root = true | ||
|
||
[*] | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 4 | ||
|
||
[*.{csproj,json,config,yml}] | ||
indent_size = 2 | ||
|
||
[*.sh] | ||
end_of_line = lf | ||
|
||
[*.{cmd, bat}] | ||
end_of_line = crlf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerAuditSink.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// Copyright 2018 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Serilog.Core; | ||
using Serilog.Debugging; | ||
using Serilog.Events; | ||
using System; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Serilog.Sinks.MSSqlServer | ||
{ | ||
/// <summary> | ||
/// Writes log events as rows in a table of MSSqlServer database using Audit logic, meaning that each row is synchronously committed | ||
/// and any errors that occur are propagated to the caller. | ||
/// </summary> | ||
public class MSSqlServerAuditSink : ILogEventSink, IDisposable | ||
{ | ||
private readonly MSSqlServerSinkTraits _traits; | ||
|
||
/// <summary> | ||
/// Construct a sink posting to the specified database. | ||
/// </summary> | ||
/// <param name="connectionString">Connection string to access the database.</param> | ||
/// <param name="tableName">Name of the table to store the data in.</param> | ||
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param> | ||
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> | ||
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param> | ||
/// <param name="columnOptions">Options that pertain to columns</param> | ||
public MSSqlServerAuditSink( | ||
string connectionString, | ||
string tableName, | ||
IFormatProvider formatProvider, | ||
bool autoCreateSqlTable = false, | ||
ColumnOptions columnOptions = null, | ||
string schemaName = "dbo" | ||
) | ||
{ | ||
if (columnOptions != null) | ||
{ | ||
if (columnOptions.DisableTriggers) | ||
throw new NotSupportedException($"The {nameof(ColumnOptions.DisableTriggers)} option is not supported for auditing."); | ||
} | ||
_traits = new MSSqlServerSinkTraits(connectionString, tableName, schemaName, columnOptions, formatProvider, autoCreateSqlTable); | ||
|
||
|
||
} | ||
|
||
/// <summary>Emit the provided log event to the sink.</summary> | ||
/// <param name="logEvent">The log event to write.</param> | ||
public void Emit(LogEvent logEvent) | ||
{ | ||
try | ||
{ | ||
using (SqlConnection connection = new SqlConnection(_traits.ConnectionString)) | ||
{ | ||
connection.Open(); | ||
using (SqlCommand command = connection.CreateCommand()) | ||
{ | ||
command.CommandType = CommandType.Text; | ||
|
||
StringBuilder fieldList = new StringBuilder($"INSERT INTO [{_traits.SchemaName}].[{_traits.TableName}] ("); | ||
StringBuilder parameterList = new StringBuilder(") VALUES ("); | ||
|
||
int index = 0; | ||
foreach (var field in _traits.GetColumnsAndValues(logEvent)) | ||
{ | ||
if (index != 0) | ||
{ | ||
fieldList.Append(','); | ||
parameterList.Append(','); | ||
} | ||
|
||
fieldList.Append(field.Key); | ||
parameterList.Append("@P"); | ||
parameterList.Append(index); | ||
|
||
SqlParameter parameter = new SqlParameter($"@P{index}", field.Value ?? DBNull.Value); | ||
|
||
// The default is SqlDbType.DateTime, which will truncate the DateTime value if the actual | ||
// type in the database table is datetime2. So we explicitly set it to DateTime2, which will | ||
// work both if the field in the table is datetime and datetime2, which is also consistent with | ||
// the behavior of the non-audit sink. | ||
if (field.Value is DateTime) | ||
parameter.SqlDbType = SqlDbType.DateTime2; | ||
|
||
command.Parameters.Add(parameter); | ||
|
||
index++; | ||
} | ||
|
||
parameterList.Append(')'); | ||
fieldList.Append(parameterList.ToString()); | ||
|
||
command.CommandText = fieldList.ToString(); | ||
|
||
command.ExecuteNonQuery(); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
SelfLog.WriteLine("Unable to write log event to the database due to following error: {1}", ex.Message); | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Releases the unmanaged resources used by the Serilog.Sinks.MSSqlServer.MSSqlServerAuditSink and optionally | ||
/// releases the managed resources. | ||
/// </summary> | ||
/// <param name="disposing">True to release both managed and unmanaged resources; false to release only unmanaged | ||
/// resources.</param> | ||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
_traits.Dispose(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |
Oops, something went wrong.