I am using Serilog with MSSqlServer sink. Even though I followed all the steps mentioned in Serilog.Sinks.MSSqlServer still I am unable to log any message in SQL Table. I really appreciate if you could tell me what part do I missed or configured incorrectly?
Here is the part the configuration code from my project:
public ILogger Logger = null;
private ColumnOptions _columnOptions = new ColumnOptions
{
AdditionalDataColumns = new Collection<DataColumn>
{
new DataColumn() { AllowDBNull = true, ColumnName = "CreatedBy",DataType = typeof (Guid) },
new DataColumn() { AllowDBNull = true, ColumnName = "CreatedDate",DataType = typeof (DateTime)},
new DataColumn() { AllowDBNull = true, ColumnName = "StatusID",DataType = typeof (byte)},
new DataColumn() { AllowDBNull = true, ColumnName = "ModifiedBy",DataType = typeof (Guid) },
new DataColumn() { AllowDBNull = true, ColumnName = "ModifiedDate",DataType = typeof (DateTime) },
new DataColumn() { AllowDBNull = true, ColumnName = "Version",DataType = typeof (Guid) },
new DataColumn() { AllowDBNull = true, ColumnName = "SessionID", DataType = typeof(string) },
new DataColumn() { AllowDBNull = true, ColumnName = "Username", DataType = typeof(string) },
new DataColumn() { AllowDBNull = true, ColumnName = "IsAuthenticated", DataType = typeof(bool) },
new DataColumn() { AllowDBNull = true, ColumnName = "ClientIPAddress", DataType = typeof(string) },
new DataColumn() { AllowDBNull = true, ColumnName = "ControllerName", DataType = typeof(string) },
new DataColumn() { AllowDBNull = true, ColumnName = "ActionName", DataType = typeof(string) },
new DataColumn() { AllowDBNull = true, ColumnName = "GetParameters", DataType = typeof(string) },
new DataColumn() { AllowDBNull = true, ColumnName = "Request", DataType = typeof(string) },
},
};
Logger = new LoggerConfiguration().WriteTo.MSSqlServer(
connectionString: ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString(),
period: TimeSpan.Zero,
batchPostingLimit: 5,
autoCreateSqlTable: false,
tableName: "Logs",
restrictedToMinimumLevel: LogEventLevel.Verbose,
columnOptions: _columnOptions)
.CreateLogger();
Here is the message template that I'm using:
public const string AuditMessageTemplate = "{SessionID}, {Username}, {IsAuthenticated}, {ClientIPAddress}, {ControllerName}, {ActionName}, {GetParameters}, {Request}, {CreatedBy}, {CreatedDate}, {StatusID}, {ModifiedBy}, {ModifiedDate}, {Version}";
And for testing it I write the following code:
for (int i = 0; i < 200; i++)
{
AuditLogger.Instance.Information(LoggerParameters.AuditMessageTemplate, auditLog.SessionID,auditLog.Username, auditLog.IsAuthenticated, auditLog.ClientIPAddress, auditLog.ControllerName,auditLog.ActionName, auditLog.GetParameters, auditLog.Request, auditLog.CreatedBy, auditLog.CreatedDate, auditLog.StatusID, auditLog.ModifiedBy, auditLog.ModifiedDate, auditLog.Version);
}
Here is some run-time information:
Here are the assemblies that I am using:
- Serilog 1.5.0.0
- Serilog.FullNetFx 1.5.0.0
- Serilog.Sinks.MSSqlServer 3.0.0.0