private void OnError( Exception exception, DbCommand command, Guid contextId, DateTime start, DateTime now, double milliseconds, Func <Exception, string, DbParameterCollection, DateTime, DateTime, object> specificErrorAction, string memberName) { var localFunc = _descriptor.ExposeErrorInterceptor; localFunc += specificErrorAction; var userInfo = localFunc?.Invoke(exception, command.CommandText, command.Parameters, start, now) ?? string.Empty; var logTrack = LogTrack.Create(contextId, EventIdKeys.Error); var logger = _loggingServiceProvider.GetLogger <DbContext>(LogEventSendMode.Automatic, _descriptor.ExposeSettings.GetRenderingOptions()); var dbParams = new List <DbParam>(); foreach (DbParameter param in command.Parameters) { dbParams.Add(new DbParam(param.ParameterName, param.Value, param.DbType)); } var realExcepton = exception.Unwrap(); var loggingParams = new { OrmName = Constants.SinkKey, ContextId = contextId, Sql = command.CommandText, SqlParams = dbParams, ExceptionType = exception.GetType(), ExceptionMessage = exception.Message, RealExceptionType = realExcepton.GetType(), RealExceptionMessage = realExcepton.Message, UsedTime = milliseconds, UserInfo = userInfo }; logger.LogError(logTrack, exception, OrmTemplateStandard.Error, loggingParams, memberName: memberName); }
private void OnLongTimeExecuted( DbCommand command, Guid contextId, DateTime start, DateTime now, double milliseconds, Func <string, DbParameterCollection, DateTime, DateTime, object> specificLongTimeExecutedAction, string memberName) { var localFunc = _descriptor.ExposeLongTimeExecutedInterceptor; localFunc += specificLongTimeExecutedAction; var userInfo = localFunc?.Invoke(command.CommandText, command.Parameters, start, now) ?? string.Empty; var logTrack = LogTrack.Create(contextId, EventIdKeys.LongTimeExecuted); var logger = _loggingServiceProvider.GetLogger <DbContext>(LogEventSendMode.Automatic, _descriptor.ExposeSettings.GetRenderingOptions()); var dbParams = new List <DbParam>(); foreach (DbParameter param in command.Parameters) { dbParams.Add(new DbParam(param.ParameterName, param.Value, param.DbType)); } var loggingParams = new { OrmName = Constants.SinkKey, ContextId = contextId, Sql = command.CommandText, SqlParams = dbParams, UsedTime = milliseconds, UserInfo = userInfo }; logger.LogInformation(logTrack, OrmTemplateStandard.LongNormal, loggingParams, memberName: memberName); }
public void WriteLog(string sqlText, string suffixEventName, [CallerMemberName] string memberName = null) { var userInfo = _simgleLoggingAct?.Invoke(sqlText) ?? string.Empty; var logTrack = LogTrack.Create($"{EventIdKeys.SqlExposure}_{suffixEventName}"); var loggingObj = new { OrmName = Constants.SinkKey, Sql = sqlText, UserInfo = userInfo }; _logger.LogDebug(logTrack, OrmTemplateStandard.SimpleSqlLog, loggingObj, memberName); }
private static void InternalExecutedOpt( SqlSugarInterceptorDescriptor descriptor, SqlSugarClient client, string sql, SugarParameter[] @params, Func<string, SugarParameter[], object> executedAct = null, Func<string, LogEventLevel, bool> filter = null) { var ms = 0D; if (client.TempItems.TryGetValue(TimestampKey, out var startStamp) && startStamp is DateTime stamp) { client.TempItems.Remove(TimestampKey); ms = DateTime.Now.Subtract(stamp).TotalMilliseconds; } object loggingParams; var userInfo = executedAct?.Invoke(sql, @params) ?? string.Empty; var logger = descriptor.ExposeLoggingServiceProvider.GetLogger<SqlSugarClient>(filter, LogEventSendMode.Automatic, descriptor.RenderingOptions); if (ms > 1000) { if (!logger.IsEnabled(LogEventLevel.Warning)) return; var logTrack = LogTrack.Create(client.ContextID, EventIdKeys.LongTimeExecuted); loggingParams = new { OrmName = Constants.SinkKey, ContextId = client.ContextID, Sql = sql, SqlParams = @params.Select(param => new DbParam(param.ParameterName, param.Value, param.DbType)).ToList(), UsedTime = ms, UserInfo = userInfo }; logger.LogWarning(logTrack, OrmTemplateStandard.LongNormal, loggingParams); } else { if (!logger.IsEnabled(LogEventLevel.Information)) return; var logTrack = LogTrack.Create(client.ContextID, EventIdKeys.Executed); loggingParams = new { OrmName = Constants.SinkKey, ContextId = client.ContextID, Sql = sql, UsedTime = ms, UserInfo = userInfo }; logger.LogInformation(logTrack, OrmTemplateStandard.Normal, loggingParams); } }
private static void InternalErrorOpt( SqlSugarInterceptorDescriptor descriptor, SqlSugarClient client, Exception exception, Func<Exception, object> errorAct = null, Func<string, LogEventLevel, bool> filter = null) { var ms = 0D; if (client.TempItems.TryGetValue(TimestampKey, out var startStamp) && startStamp is DateTime stamp) { client.TempItems.Remove(TimestampKey); ms = DateTime.Now.Subtract(stamp).TotalMilliseconds; } object userInfo = errorAct?.Invoke(exception) ?? string.Empty; var logger = descriptor.ExposeLoggingServiceProvider.GetLogger<SqlSugarClient>(filter, LogEventSendMode.Automatic, descriptor.RenderingOptions); if (!logger.IsEnabled(LogEventLevel.Error)) return; var logTrack = LogTrack.Create(client.ContextID, EventIdKeys.Error); var realException = exception.Unwrap(); var loggingParams = new { OrmName = Constants.SinkKey, ContextId = client.ContextID, Sql = "unknown", SqlParams = "unknown", ExceptionType = exception.GetType(), ExceptionMessage = exception.Message, RealExceptionType = realException.GetType(), RealExceptionMessage = realException.Message, UsedTime = ms, UserInfo = userInfo }; logger.LogError(logTrack, exception, OrmTemplateStandard.Error, loggingParams); }
/// <summary> /// Convert Microsoft Event Id to Cosmos Logging Business Track Info /// </summary> /// <param name="eventId"></param> /// <returns></returns> public static LogTrack ToTrackInfo(this EventId eventId) => LogTrack.Create(eventId.Id, eventId.Name);
private static void InternalExecutedOpt( FreeSqlInterceptorDescriptor descriptor, CurdAfterEventArgs args, Func <CurdAfterEventArgs, object> executedAct = null, Func <Exception, CurdAfterEventArgs, object> errorAct = null, Func <string, LogEventLevel, bool> filter = null) { object loggingParams; var errorFlag = args.Exception != null; var userInfo = errorFlag ? errorAct?.Invoke(args.Exception, args) ?? string.Empty : executedAct?.Invoke(args) ?? string.Empty; var logger = descriptor.ExposeLoggingServiceProvider.GetLogger <IAdo>(filter, LogEventSendMode.Automatic, descriptor.RenderingOptions); if (!errorFlag && args.ElapsedMilliseconds > 1000) { if (!logger.IsEnabled(LogEventLevel.Warning)) { return; } var logTrack = LogTrack.Create(args.Identifier, EventIdKeys.LongTimeExecuted); loggingParams = new { OrmName = Constants.SinkKey, ContextId = args.Identifier, args.Sql, SqlParams = args.DbParms.Select(param => new DbParam(param.ParameterName, param.Value, param.DbType)).ToList(), UsedTime = args.ElapsedMilliseconds, UserInfo = userInfo }; logger.LogWarning(logTrack, OrmTemplateStandard.LongNormal, loggingParams); } else if (!errorFlag) { if (!logger.IsEnabled(LogEventLevel.Information)) { return; } var logTrack = LogTrack.Create(args.Identifier, EventIdKeys.Executed); loggingParams = new { OrmName = Constants.SinkKey, ContextId = args.Identifier, args.Sql, UsedTime = args.ElapsedMilliseconds, UserInfo = userInfo }; logger.LogInformation(logTrack, OrmTemplateStandard.Normal, loggingParams); } else { if (!logger.IsEnabled(LogEventLevel.Error)) { return; } var logTrack = LogTrack.Create(args.Identifier, EventIdKeys.Error); var exception = args.Exception; var realException = exception.Unwrap(); loggingParams = new { OrmName = Constants.SinkKey, ContextId = args.Identifier, args.Sql, SqlParams = args.DbParms.Select(param => new DbParam(param.ParameterName, param.Value, param.DbType)).ToList(), ExceptionType = exception.GetType(), ExceptionMessage = exception.Message, RealExceptionType = realException.GetType(), RealExceptionMessage = realException.Message, UsedTime = args.ElapsedMilliseconds, UserInfo = userInfo }; logger.LogError(logTrack, exception, OrmTemplateStandard.Error, loggingParams); } }