src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerAuditSink.cs
/// <summary>Emit the provided log event to the sink.</summary>
/// <param name="logEvent">The log event to write.</param>
Emit the provided log event to the sink.
Emit the provided log event to the sink .
public void Emit(LogEvent logEvent)
        {
            try
            {
                using (var connection = _sqlConnectionFactory.Create())
                {
                    connection.Open();
                    using (SqlCommand command = connection.CreateCommand())
                    {
                        command.CommandType = CommandType.Text;
                        var fieldList = new StringBuilder($"INSERT INTO [{_traits.SchemaName}].[{_traits.TableName}] (");
                        var parameterList = new StringBuilder(") VALUES (");
                        var 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);
                            var parameter = new SqlParameter($"@P{index}", field.Value ?? DBNull.Value);
                            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;
            }
        }
public void Emit ( LogEvent logEvent ) { try { using ( var connection = _sqlConnectionFactory . Create ( ) ) { connection . Open ( ) ; using ( SqlCommand command = connection . CreateCommand ( ) ) { command . CommandType = CommandType . Text ; var fieldList = new StringBuilder ( $" INSERT INTO [ { _traits . SchemaName } ].[ { _traits . TableName } ] ( " ) ; var parameterList = new StringBuilder ( " " ) ; var index = 0 ; foreach ( var field in _traits . GetColumnsAndValues ( logEvent ) ) { if ( index != 0 ) { fieldList . Append ( ' ' ) ; parameterList . Append ( ' ' ) ; } fieldList . Append ( field . Key ) ; parameterList . Append ( " " ) ; parameterList . Append ( index ) ; var parameter = new SqlParameter ( $" @P { index } " , field . Value ?? DBNull . Value ) ; 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 ( " " , ex . Message ) ; throw ; } }
Emit the provided log event to the sink.
Emit the provided log event to the sink .
// 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.
logEvent
LogEvent
The log event to write.
The log event to write .