5

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:

locals windows of visual studio

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
ssmsexe
  • 209
  • 2
  • 4
  • 10
  • 1
    Can you please hook up `SelfLog` and attach any output from there? Instructions at: https://github.com/serilog/serilog/wiki/Debugging-and-Diagnostics - thanks! – Nicholas Blumhardt Feb 03 '16 at 21:33
  • Thanks to your comment @NicholasBlumhardt I've managed to find the root cause and solve the problem. This was the exceptions that logged: System.ArgumentException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).Couldn't store <"00000000-0000-0000-0000-000000000000"> in CreatedBy Column. Thanks again. – ssmsexe Feb 04 '16 at 01:37
  • @NicholasBlumhardt I hooked up to the self log and it's not outputting anything at all. (i'm having the same problem). Anything you've run into? – Sinaesthetic Sep 20 '16 at 20:54

2 Answers2

0

Good day,

I had the same problem when adding additional columns to the log table in my database. What I found was, when defining a data column object of date type Guid, it will not log. Once I changed the data type to string, it worked perfectly fine.

Example:

new DataColumn() { AllowDBNull = true, ColumnName = "CreatedBy",DataType = typeof (Guid) },

vs

new DataColumn() { AllowDBNull = true, ColumnName = "CreatedBy",DataType = typeof (string) },

I know this is not ideal, because I too would like to use Guid instead of string.

0

If someone is stuck with the same issue, I have just found out that if you add a column named "ActionName" in the AdditionalColumns nothing is logged anymore (and no error message appears in debug)

Just renamed it to "Action" and it works.

But it might be related to another package I use (Serilog.AspNetCore)

user1756338
  • 211
  • 2
  • 13