/// <summary> /// log event will be appended over broker /// </summary> /// <param name="logEvent"></param> protected override void Write(LogEventInfo logEvent) { try { var topic = Topic.Render(logEvent); var logMessage = Layout.Render(logEvent); var bytes = Encoding.ASCII.GetBytes(logMessage); _producer.Produce(ref topic, ref bytes); } catch (ProduceException <Null, string> ex) { if (ex.Error.IsFatal) { if (!_recovering) { lock (Locker) { if (!_recovering) { _recovering = true; try { _producer?.Dispose(); } catch (Exception ex2) { if (Debug) { Console.WriteLine(ex2.ToString()); } } if (Async) { _producer = new KafkaProducerAsync(Brokers, LingerMs); } else { _producer = new KafkaProducerSync(Brokers, LingerMs); } _recovering = false; } } } } } catch (Exception ex) { if (Debug) { Console.WriteLine(ex.ToString()); } } }
/// <summary> /// initializeTarget /// </summary> protected override void InitializeTarget() { base.InitializeTarget(); try { if (string.IsNullOrEmpty(Brokers)) { //throw new BrokerNotFoundException("Broker is not found"); Console.WriteLine("Broker is not found"); throw new BrokerNotFoundException("Broker is not found"); } if (_producer != null) { return; } lock (Locker) { if (_producer != null) { return; } if (Async) { _producer = new KafkaProducerAsync(Brokers, LingerMs); } else { _producer = new KafkaProducerSync(Brokers, LingerMs); } } } catch (Exception ex) { if (Debug) { Console.WriteLine(ex.ToString()); } base.CloseTarget(); } }