2016-02-03 1 views
5

Ich verwende Serilog mit MSSqlServer-Senke. Obwohl ich alle in Serilog.Sinks.MSSqlServer erwähnten Schritte befolgte, kann ich immer noch keine Nachricht in der SQL-Tabelle protokollieren. Ich weiß es wirklich zu schätzen, wenn Sie mir sagen könnten, welchen Teil ich verpasst oder falsch konfiguriert habe? HierMS SQL konnte mit Serilog nicht eingeloggt werden

ist der Teil der Konfigurationscode von meinem Projekt:

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(); 

Hier ist die Nachricht Vorlage, die ich verwende:

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

Und zum Testen es schreibe ich den folgenden 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); 
} 

Hier einige Laufzeit-Informationen:

locals windows of visual studio

Hier sind die Baugruppen, die ich verwende:

  • Serilog 1.5.0.0
  • Serilog.FullNetFx 1.5.0.0
  • Serilog.Sinks.MSSqlServer 3.0.0.0
+1

Können Sie bitte 'SelfLog' anschließen und irgendeinen Ausgang von dort anschließen? Anleitung unter: https://github.com/serilog/serilog/wiki/Debugging-and-Diagnostics - danke! –

+0

Dank deinem Kommentar @NicholasBlumhardt habe ich es geschafft, die Ursache zu finden und das Problem zu lösen. Dies waren die Ausnahmen, die protokolliert wurden: System.ArgumentException: Guid sollte 32 Ziffern mit 4 Bindestrichen enthalten (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx). Kann <"00000000-0000-0000-0000-00000000000000"> nicht speichern in CreatedBy Spalte. Danke nochmal. – DeveloperX

+0

@NicholasBlumhardt Ich habe mich an das Self Log angeschlossen und es gibt überhaupt nichts aus. (Ich habe das gleiche Problem). Alles, was Sie getroffen haben? – Sinaesthetic

Antwort

0

Guten Tag,

Ich hatte das gleiche Problem, wenn ich der Protokolltabelle in meiner Datenbank zusätzliche Spalten hinzufügte. Was ich gefunden habe, war, wenn ein Datenspaltenobjekt vom Datumstyp Guid definiert wird, wird es nicht protokollieren. Nachdem ich den Datentyp in string geändert habe, funktionierte es einwandfrei.

Beispiel:

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

vs

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

Ich weiß, das ist nicht ideal, weil ich Guid auch möchte statt String verwenden.

Verwandte Themen