/// <summary> /// log event will be appended over broker /// </summary> /// <param name="logEvent"></param> protected override void Write(LogEventInfo logEvent) { try { string topic = this.Topic.Render(logEvent); string logMessage = this.Layout.Render(logEvent); producer.Produce(ref topic, ref logMessage); } 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); } else { producer = new KafkaProducerSync(Brokers); } recovering = false; } } } } } catch (Exception ex) { if (Debug) { Console.WriteLine(ex.ToString()); } } }
/// <summary> /// disposing the target /// </summary> protected override void CloseTarget() { base.CloseTarget(); try { _producer?.Dispose(); _producer = null; } catch (Exception ex) { if (Debug) { Console.WriteLine(ex.ToString()); } } }
/// <summary> /// initializeTarget /// </summary> protected override void InitializeTarget() { base.InitializeTarget(); try { if (Brokers == null || Brokers.Length == 0) { throw new BrokerNotFoundException("Broker is not found"); } if (producer == null) { lock (_locker) { if (producer == null) { if (Async) { producer = new KafkaProducerAsync(Brokers); } else { producer = new KafkaProducerSync(Brokers); } } } } } catch (Exception ex) { if (Debug) { Console.WriteLine(ex.ToString()); } base.CloseTarget(); } }