? C#亚博足球娱乐场注册--任意三数字加yabo.com直达官网之[开源]基于Log4Net简单实现KafkaAppender 亚博足球娱乐场注册,亚博娱乐全天彩,亚博官网赢钱不给
VB.net 2010 视频亚博足球娱乐场注册--任意三数字加yabo.com直达官网 VB.net 2010 视频亚博足球娱乐场注册--任意三数字加yabo.com直达官网 VB.net 2010 视频亚博足球娱乐场注册--任意三数字加yabo.com直达官网
SQL Server 2008 视频亚博足球娱乐场注册--任意三数字加yabo.com直达官网 c#入门经典亚博足球娱乐场注册--任意三数字加yabo.com直达官网 Visual Basic从门到精通视频亚博足球娱乐场注册--任意三数字加yabo.com直达官网
  • C#亚博足球娱乐场注册--任意三数字加yabo.com直达官网之[开源]基于Log4Net简单实现KafkaAppender

  • 2019-04-13 19:26 来源:未知

背景#

  1. 基于之前基于Log4Net本地日志服务简单实现?实现本地日志服务,但是随着项目开发演进,本地日志服务满足不了需求,譬如在预发布环境或者生产环境,不可能让开发人员登录查看本地日志文件分析。
  2. Kafka+ELK日志服务套件,可以在线日志服务可以解决上述问题,并且提供丰富报表分析等等;
  3. 具体源码:MasterChief
  4. Nuget:Install-Package MasterChief.DotNet.Core.KafkaLog
  5. 欢迎Star,欢迎Issues;

源码#

  1. 基于Log4Net来实现与kafka通讯Appender

    
    			
    Copy
    public class KafkaAppender : AppenderSkeleton { #region Fields /// /// Kafka 生产者 /// private Producer _kafkaProducer; #endregion Fields #region Properties /// /// Brokers /// public string Brokers { get; set; } /// /// Topic /// public string Topic { get; set; } #endregion Properties #region Methods /// /// Initialize the appender based on the options set /// /// /// /// This is part of the delayed object /// activation scheme. The method must /// be called on this object after the configuration properties have /// been set. Until is called this /// object is in an undefined state and must not be used. /// /// /// If any of the configuration properties are modified then /// must be called again. /// /// public override void ActivateOptions() { base.ActivateOptions(); InitKafkaProducer(); } /// /// Subclasses of should implement this method /// to perform actual logging. /// /// The event to append. /// /// /// A subclass must implement this method to perform /// logging of the . /// /// /// This method will be called by /// if all the conditions listed for that method are met. /// /// /// To restrict the logging of events in the appender /// override the method. /// /// protected override void Append(LoggingEvent loggingEvent) { try { var message = GetLogMessage(loggingEvent); var topic = GetTopic(loggingEvent); _ = _kafkaProducer.SendMessageAsync(topic, new[] {new Message(message)}); } catch (Exception ex) { ErrorHandler.Error("KafkaProducer SendMessageAsync", ex); } } /// /// Raises the Close event. /// /// /// /// Releases any resources allocated within the appender such as file handles, /// network connections, etc. /// /// /// It is a programming error to append to a closed appender. /// /// protected override void OnClose() { base.OnClose(); StopKafkaProducer(); } private string GetLogMessage(LoggingEvent loggingEvent) { var builder = new StringBuilder(); using (var writer = new StringWriter(builder)) { Layout.Format(writer, loggingEvent); if (Layout.IgnoresException && loggingEvent.ExceptionObject != null) writer.Write(loggingEvent.GetExceptionString()); return writer.ToString(); } } private string GetTopic(LoggingEvent loggingEvent) { return string.IsNullOrEmpty(Topic) ? Path.GetFileNameWithoutExtension(loggingEvent.Domain) : Topic; } /// /// 初始化Kafka 生产者 /// private void InitKafkaProducer() { try { if (string.IsNullOrEmpty(Brokers)) Brokers = "http://localhost:9200"; if (_kafkaProducer == null) { var brokers = new Uri(Brokers); var kafkaOptions = new KafkaOptions(brokers) { Log = new KafkaLog() }; _kafkaProducer = new Producer(new BrokerRouter(kafkaOptions)); } } catch (Exception ex) { ErrorHandler.Error("InitKafkaProducer", ex); } } /// /// 停止生产者 /// private void StopKafkaProducer() { try { _kafkaProducer?.Stop(); } catch (Exception ex) { ErrorHandler.Error("StopKafkaProducer", ex); } } #endregion Methods }
  2. 基于之前定义接口,来实现kafkaLogService

    
    			
    Copy
    public sealed class KafkaLogService : ILogService { #region Constructors /// /// Initializes the class. /// static KafkaLogService() { KafkaLogger = LogManager.GetLogger(KafkaLoggerName); } #endregion Constructors #region Fields /// /// Kafka logger name /// public const string KafkaLoggerName = "KafkaLogger"; /// /// Kafka logger /// public static readonly ILog KafkaLogger; #endregion Fields #region Methods /// /// Debug记录 /// /// 日志信息 public void Debug(string message) { if (KafkaLogger.IsDebugEnabled) KafkaLogger.Debug(message); } /// /// Debug记录 /// /// 日志信息 /// 异常信息 public void Debug(string message, Exception ex) { if (KafkaLogger.IsDebugEnabled) KafkaLogger.Debug(message, ex); } /// /// Error记录 /// /// 日志信息 public void Error(string message) { if (KafkaLogger.IsErrorEnabled) KafkaLogger.Error(message); } /// /// Error记录 /// /// 日志信息 /// 异常信息 public void Error(string message, Exception ex) { if (KafkaLogger.IsErrorEnabled) KafkaLogger.Error(message, ex); } /// /// Fatal记录 /// /// 日志信息 public void Fatal(string message) { if (KafkaLogger.IsFatalEnabled) KafkaLogger.Fatal(message); } /// /// Fatal记录 /// /// 日志信息 /// 异常信息 public void Fatal(string message, Exception ex) { if (KafkaLogger.IsFatalEnabled) KafkaLogger.Fatal(message, ex); } /// /// Info记录 /// /// 日志信息 public void Info(string message) { if (KafkaLogger.IsInfoEnabled) KafkaLogger.Info(message); } /// /// Info记录 /// /// 日志信息 /// 异常信息 public void Info(string message, Exception ex) { if (KafkaLogger.IsInfoEnabled) KafkaLogger.Info(message, ex); } /// /// Warn记录 /// /// 日志信息 public void Warn(string message) { if (KafkaLogger.IsWarnEnabled) KafkaLogger.Warn(message); } /// /// Warn记录 /// /// 日志信息 /// 异常信息 public void Warn(string message, Exception ex) { if (KafkaLogger.IsWarnEnabled) KafkaLogger.Warn(message, ex); } #endregion Methods }
  3. 修改Log4Net.Config,定义Kafka的Topic以及Brokers

    
    			
    Copy
    <appender name="KafkaAppender" type="MasterChief.DotNet.Core.KafkaLog.KafkaAppender, MasterChief.DotNet.Core.KafkaLog"> <param name="Topic" value="beats" /> <param name="Brokers" value="http://localhost:9092" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="发生时间:%date %newline事件级别:%-5level %newline事件来源:%logger%newline日志内容:%message%newline" /> layout> appender>

使用#

  1. 由于基于上篇说的日志接口,所以可以通过Ioc切换,而且不影响在业务代码调用;
  2. 基于业务需求,您可以同时落地本地日志,保证网络抖动或者不正常的时候能够正常记录日志;

结语#

  1. 小弟不才,大佬轻拍;

作者:YanZhiwei

出处:https://www.cnblogs.com/MeetYan/p/10693545.html

本站使用「署名 4.0 国际」创作共享协议,转载请在文章明显位置注明作者及出处。


相关亚博足球娱乐场注册--任意三数字加yabo.com直达官网