/// <summary> /// Creates a record to the security log /// </summary> /// <param name="recordType">The record type</param> /// <param name="severity">The data severity</param> /// <param name="requestContext">The request description</param> /// <param name="format">The message format</param> /// <param name="parameters">Additional message parameters</param> public static void CreateRecord( EnSecurityLogType recordType, EnSeverity severity, RequestContext requestContext, string format, params object[] parameters) { MessageTemplate template; IEnumerable <LogEventProperty> properties; if (!Serilog.Log.BindMessageTemplate(format, parameters, out template, out properties)) { throw new InvalidOperationException("Incorrect message format"); } var record = new LogEvent(DateTimeOffset.Now, GetLogLevel(recordType, severity), null, template, properties); record.AddOrUpdateProperty( new LogEventProperty(Constants.LogRecordTypeKey, new ScalarValue(EnLogRecordType.Security))); record.AddPropertyIfAbsent(new LogEventProperty("SecurityRecordType", new ScalarValue(recordType))); record.AddPropertyIfAbsent(new LogEventProperty("SecuritySeverity", new ScalarValue(severity))); if (requestContext != null) { record.AddPropertyIfAbsent(new LogEventProperty("SecurityRequest", CreateLogValue(requestContext))); } Serilog.Log.Logger.Write(record); }
/// <summary> /// Gets the log record level by it's type and severity /// </summary> /// <param name="recordType">The record type</param> /// <param name="severity">The data severity</param> /// <returns>The log record level</returns> private static LogEventLevel GetLogLevel(EnSecurityLogType recordType, EnSeverity severity) { switch (severity) { case EnSeverity.Trivial: switch (recordType) { case EnSecurityLogType.DataReadGranted: case EnSecurityLogType.OperationGranted: return(LogEventLevel.Debug); case EnSecurityLogType.DataCreateGranted: case EnSecurityLogType.DataUpdateGranted: case EnSecurityLogType.DataDeleteGranted: case EnSecurityLogType.AuthenticationGranted: return(LogEventLevel.Information); case EnSecurityLogType.AuthenticationDenied: return(LogEventLevel.Warning); case EnSecurityLogType.OperationDenied: return(LogEventLevel.Error); default: throw new ArgumentOutOfRangeException(nameof(recordType), recordType, null); } case EnSeverity.Crucial: switch (recordType) { case EnSecurityLogType.DataReadGranted: case EnSecurityLogType.OperationGranted: case EnSecurityLogType.DataCreateGranted: case EnSecurityLogType.DataUpdateGranted: case EnSecurityLogType.DataDeleteGranted: case EnSecurityLogType.AuthenticationGranted: return(LogEventLevel.Information); case EnSecurityLogType.AuthenticationDenied: return(LogEventLevel.Warning); case EnSecurityLogType.OperationDenied: return(LogEventLevel.Error); default: throw new ArgumentOutOfRangeException(nameof(recordType), recordType, null); } default: throw new ArgumentOutOfRangeException(nameof(severity), severity, null); } }