Пример #1
0
 public void ConfigureLogger(DatabaseLoggerSettings settings)
 {
     this.settings = settings;
     this.service  = new LoggerService(settings);
 }
Пример #2
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            var message = formatter(state, exception);

            var isPerformaInfo = message.StartsWith($"_%%%Info prefix%%%_");

            if (logLevel < this.settings.MinimumLogLevel && !isPerformaInfo)
            {
                return;
            }

            if (isPerformaInfo)
            {
                message = message.Substring(message.IndexOf("_%%%Info prefix%%%_") + 19, message.Length - message.IndexOf("_%%%Info prefix%%%_") - 19);
            }
            string exceptionMsg = null;

            if (exception != null)
            {
                exceptionMsg = exception.StackTrace;
            }
            LoggerService service = new LoggerService(this.settings);

            string tenantId    = null;
            string userId      = null;
            int    errorCode   = int.MinValue;
            var    realMessage = message;

            try
            {
                var hasTenantId  = message.Contains("_&&&") && message.Contains("&&&_");
                var hasUserId    = message.Contains("_$$$") && message.Contains("$$$_");
                var hasErrorCode = message.Contains("_###") && message.Contains("###_");

                tenantId  = hasTenantId ? ExtrudeTenantId(message) : null;
                userId    = hasUserId ? ExtrudeUserId(message) : null;
                errorCode = hasErrorCode ? ExtrudeErrorCode(message) : int.MinValue;

                if (!string.IsNullOrEmpty(tenantId))
                {
                    var tenantIdPrefix = $"_&&&{tenantId}&&&_";
                    message = message.Remove(message.IndexOf(tenantIdPrefix), tenantIdPrefix.Length);
                }
                if (!string.IsNullOrEmpty(userId))
                {
                    var userIdPrefix = $"_$$${userId}$$$_";
                    message = message.Remove(message.IndexOf(userIdPrefix), userIdPrefix.Length);
                }
                if (errorCode != int.MinValue)
                {
                    var errorCodePrefix = $"_###{errorCode}###_";
                    message = message.Remove(message.IndexOf(errorCodePrefix), errorCodePrefix.Length);
                }
                realMessage = message;
            }
            catch (Exception)
            {
                tenantId  = null;
                userId    = null;
                errorCode = int.MinValue;
            }


            if (exception != null)
            {
                realMessage += " MESSAGE: " + exception.Message;
            }

            service.Write(realMessage, GetLogLevelInt(logLevel), exceptionMsg, errorCode, this.categoryName, eventId.Id, tenantId, userId);
        }