Failed to log into MS SQL using Serilog

I use Serilog with the MSSqlServer shell. Despite the fact that I followed all the steps mentioned in Serilog.Sinks.MSSqlServer , I still can not register any message in the SQL table. I really appreciate if you could tell me which part I missed or misconfigured?

Here is part of 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 I am using:

 public const string AuditMessageTemplate = "{SessionID}, {Username}, {IsAuthenticated}, {ClientIPAddress}, {ControllerName}, {ActionName}, {GetParameters}, {Request}, {CreatedBy}, {CreatedDate}, {StatusID}, {ModifiedBy}, {ModifiedDate}, {Version}"; 

And for testing, 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); } 

Below is the runtime information:

local windows of visual studio

Here are the builds I use:

  • Serilog 1.5.0.0
  • Serilog.FullNetFx 1.5.0.0
  • Serilog.Sinks.MSSqlServer 3.0.0.0
+5
source share
1 answer

Good afternoon,

I had the same problem when adding extra columns to the log table in my database. I found that when defining a Guid date type data column object, it will not be registered. As soon as I changed the data type to a string, it worked perfectly.

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 would also like to use Guid instead of a string.

0
source

Source: https://habr.com/ru/post/1242206/


All Articles