.NetCore 中扩展ExceptionLess 实现链式方法添加操作日志

在使用ExceptionLess添加日志的时候,发现还是有一些写法上的个人觉得不爽的地方,比如添加Info日志

  ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Info).AddTags(tags.ToArray()).Submit();

如果使用自己的添加方法可能是下面这样的 

 public void Info(string source, string message, params string[] tags)
        {
            ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Info).AddTags(tags).Submit();
        }

一旦Tags多了前面写的时候前面出现太多的字符串参数看上去不太友好

所以下面我对ExceptionLess进行了扩展,在原有的接口实现上扩展了一个IELLogExtensions

 public static class IELLogExtensions
    {

        public static IELLog AddSource(this IELLog eLLog,string source)
        {
            eLLog.Source(source);
            return eLLog;
        }
        public static IELLog AddMessage(this IELLog eLLog, string message)
        {
            eLLog.Message(message);
            return eLLog;
        }
        public static IELLog AddTag(this IELLog eLLog, string tag)
        {
            eLLog.Tags(tag);
            return eLLog;
        }
        public static IELLog AddSubmitInfo(this IELLog eLLog)
        {
            eLLog.SubmitInfo();
            return eLLog;
        }

        public static IELLog AddSubmitError(this IELLog eLLog)
        {
            eLLog.SubmitError();
            return eLLog;
        }

        public static IELLog AddSubmitDebug(this IELLog eLLog)
        {
            eLLog.SubmitDebug();
            return eLLog;
        }
        public static IELLog AddSubmitTrace(this IELLog eLLog)
        {
            eLLog.SubmitTrace();
            return eLLog;
        }
        public static IELLog AddSubmitWarn(this IELLog eLLog)
        {
            eLLog.SubmitWarn();
            return eLLog;
        }

        
            
            
    }

在将原来的接口实现修改成下面的方式 IELLog

 public interface IELLog
    {

        void Source(string source);
        void Message(string message);
        void Tags(string tag);
        void SubmitInfo();
        void SubmitError();

        void SubmitDebug();
        void SubmitTrace();
        void SubmitWarn();
}

实现下这些具体操作,定义了一些用来装载信息的容器 如source 、message 、tags

public class ExceptionlessLogService : IELLog
    {

        private string source { get; set; }
        private string message { get; set; }
        private List<string> tags = new List<string>();
        private readonly ExceptionLessConfig exceptionLessConfig;
        public ExceptionlessLogService(IOptions<ExceptionLessConfig> options)
        {
            exceptionLessConfig = options.Value;
        }


        public void Source(string source) {
            this.source = source;
        }
        public void Message(string message) {
            this.message = message;
        }
        public void Tags(string tag) {

            tags.Add(tag);
        }
        public void SubmitInfo()
        {
            ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Info).AddTags(tags.ToArray()).Submit();

        }
        public void SubmitError()
        {
            ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Error).AddTags(tags.ToArray()).Submit();

        }
        public void SubmitDebug()
        {
            ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Debug).AddTags(tags.ToArray()).Submit();

        }

        public void SubmitTrace()
        {
            ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Trace).AddTags(tags.ToArray()).Submit();

        }

        public void SubmitWarn()
        {
            ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Warn).AddTags(tags.ToArray()).Submit();

        }
}

下面使用这些扩展日志的时候了,来看看写法上有什么不一样,如下看起来很简洁清晰

          _eLLog.AddSource("Title")
                    .AddMessage("内容")
                    .AddTag("tag1")
                    .AddTag("tag2")
                    .AddTag("tag3")
                    .AddTag("tag4")
                    .AddSubmitInfo();
原文地址:https://www.cnblogs.com/liyouming/p/9809355.html