Ejemplo n.º 1
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            var message = formatter(state, exception);
            var source  = $"{_categoryName}";

            EventBuilder eventBuilder = null;

            if (exception != null)
            {
                eventBuilder = ExceptionlessClient.Default.CreateException(exception);
            }
            else
            {
                eventBuilder = ExceptionlessClient.Default.CreateLog(source, message, logLevel.ToString());
            }

            eventBuilder.SetMessage(message).SetSource(source);

            if (eventId != 0)
            {
                eventBuilder.SetProperty("enentId", eventId);
            }

            eventBuilder.Submit();
        }
 /// <summary>
 /// 设置异常
 /// </summary>
 /// <param name="builder">事件生成器</param>
 /// <param name="content">日志内容</param>
 private void SetException(EventBuilder builder, ILogContent content)
 {
     if (content.Exception == null)
     {
         return;
     }
     if (content is ICaption caption && !string.IsNullOrWhiteSpace(caption.Caption))
     {
         builder.SetMessage($"{builder.Target.Message} {content.Exception.Message}【{caption.Caption}】");
     }
     SetExceptionData(builder, content);
 }
        /// <summary>
        /// 将消息与异常写入到存储设备里
        /// </summary>
        /// <param name="level">级别</param>
        /// <param name="msg">消息</param>
        /// <param name="eventId">事件ID</param>
        /// <param name="ex">异常</param>
        /// <param name="source">来源</param>
        /// <param name="tags">标签</param>
        protected override void WriteStorage(string level, string msg, string eventId, Exception ex = null, string source = null, params string[] tags)
        {
            if (string.IsNullOrWhiteSpace(source) && ex != null)
            {
                source = ex.Source;
            }
            var      logLevel = LogLevelHelper.Parse(level);
            LogLevel exLevel  = null;

            switch (logLevel)
            {
            case LogLevelEnum.TRACE:
                exLevel = LogLevel.Trace;

                break;

            case LogLevelEnum.DEBUG:
                exLevel = LogLevel.Debug;

                break;

            case LogLevelEnum.INFO:
                exLevel = LogLevel.Info;

                break;

            case LogLevelEnum.WRAN:
                exLevel = LogLevel.Warn;

                break;

            case LogLevelEnum.ERROR:
                exLevel = LogLevel.Error;

                break;

            case LogLevelEnum.FATAL:
                exLevel = LogLevel.Fatal;

                break;

            default:

                return;
            }

            EventBuilder builder = null;

            if (ex == null)
            {
                builder = ExceptionlessClient.Default.CreateLog(source, msg, exLevel);
            }
            else
            {
                builder = ExceptionlessClient.Default.CreateException(ex);
                if (!string.IsNullOrWhiteSpace(source))
                {
                    builder.SetSource(source);
                }
                if (!string.IsNullOrWhiteSpace(msg))
                {
                    builder.SetMessage(msg);
                }
            }
            builder.AddTags(AppendLocalIdTags(eventId, tags));
            builder.Submit();
        }