From 9670b15484d59d4efb263ce53b832cf05c30711a Mon Sep 17 00:00:00 2001 From: jeremysmith1 Date: Sat, 18 Aug 2018 18:09:14 -0400 Subject: [PATCH] Fix input to send events to outputs. Updated logic for creating message. Added readme for log4net --- README.md | 46 +++++++++++++++++++ .../Log4netInput.cs | 21 ++++++--- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 079b5181..de10a0a1 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ It runs in the same process as the application, so communication overhead is min - [Microsoft.Extensions.Logging](#microsoftextensionslogging) - [ETW (Event Tracing for Windows)](#etw-event-tracing-for-windows) - [Application Insights](#application-insights-input) +- [Log4net](#Log4net) **Outputs** - [StdOutput (console output)](#stdoutput) @@ -577,6 +578,50 @@ namespace AspNetCoreEventFlow ``` +#### Log4net + +*Nuget package:* [**Microsoft.Diagnostics.EventFlow.Inputs.Log4net**](https://www.nuget.org/packages/Microsoft.Diagnostics.EventFlow.Inputs.Log4net/) + +This input enables capturing diagnostic data sent to the [Log4net project](https://logging.apache.org/log4net/). + +*Configuration example* +The Log4net input has one configuration, the Log4net Level: +```json +{ + "type": "Log4net", + "logLevel": "Debug" +} +``` +| Field | Values/Types | Required | Description | +| :---- | :-------------- | :------: | :---------- | +| `type` | "Log4net" | Yes | Specifies the output type. For this output, it must be "Log4net". | +| `logLevel` | "Debug", "Info", "Warn", "Error", or "Fatal" | Yes | Specifies the [Log4net Level](https://logging.apache.org/log4net/log4net-1.2.11/release/sdk/log4net.Core.Level.html) desired. | + + +*Example: instantiating a Log4net logger that uses EventFlow Log4net input* + +```csharp +using System; +using Microsoft.Diagnostics.EventFlow; +using log4net; + +namespace ConsoleApp2 +{ + class Program + { + static void Main(string[] args) + { + using (var pipeline = DiagnosticPipelineFactory.CreatePipeline(".\\eventFlowConfig.json")) + { + var logger = LogManager.GetLogger("EventFlowRepo", "MY_LOGGER_NAME"); + + logger.Debug("Hey! Listen!", new Exception("uhoh")); + } + } + } +} +``` + ### Outputs Outputs define where data will be published from the engine. It's an error if there are no outputs defined. Each output type has its own set of parameters. @@ -1261,6 +1306,7 @@ The following table lists platform support for standard inputs and outputs. | [Microsoft.Extensions.Logging](#microsoftextensionslogging) | Yes | Yes | Yes | | [ETW (Event Tracing for Windows)](#etw-event-tracing-for-windows) | Yes | Yes | No | | [Application Insights input](#application-insights-input) | Yes | Yes | Yes | +| [Log4net](#Log4net) | Yes | Yes | Yes | | *Outputs* | | [StdOutput (console output)](#stdoutput) | Yes | Yes | Yes | | [Application Insights](#application-insights) | Yes | Yes | Yes | diff --git a/src/Microsoft.Diagnostics.EventFlow.Inputs.Log4net/Log4netInput.cs b/src/Microsoft.Diagnostics.EventFlow.Inputs.Log4net/Log4netInput.cs index ac14efd4..565cad50 100644 --- a/src/Microsoft.Diagnostics.EventFlow.Inputs.Log4net/Log4netInput.cs +++ b/src/Microsoft.Diagnostics.EventFlow.Inputs.Log4net/Log4netInput.cs @@ -12,6 +12,8 @@ using Microsoft.Diagnostics.EventFlow.Configuration; using Microsoft.Extensions.Configuration; using Validation; +using log4net.Config; +using System.Linq; namespace Microsoft.Diagnostics.EventFlow.Inputs { @@ -70,21 +72,22 @@ private void Initialize(Log4netConfiguration myLog4NetConfig, IHealthReporter my //Can we determine if the repo exists without try/catch try { - eventFlowRepo = (Hierarchy)LogManager.CreateRepository("EventFlow"); + eventFlowRepo = (Hierarchy)LogManager.CreateRepository("EventFlowRepo"); _log4NetInputConfiguration.Log4netLevel = eventFlowRepo.LevelMap[_log4NetInputConfiguration.LogLevel]; eventFlowRepo.Root.AddAppender(this); eventFlowRepo.Configured = true; + BasicConfigurator.Configure(eventFlowRepo); } catch (LogException) { - eventFlowRepo = (Hierarchy) LogManager.GetRepository("EventFlow"); + eventFlowRepo = (Hierarchy) LogManager.GetRepository("EventFlowRepo"); } } protected override void Append(LoggingEvent loggingEvent) { - if (loggingEvent == null ||!IsEnabledFor(loggingEvent.Level)) + if (loggingEvent == null || !IsEnabledFor(loggingEvent.Level)) { return; } @@ -93,18 +96,24 @@ protected override void Append(LoggingEvent loggingEvent) this.subject.OnNext(eventData); } - private bool IsEnabledFor(Level level) => level <= _log4NetInputConfiguration.Log4netLevel; + private bool IsEnabledFor(Level level) => level >= _log4NetInputConfiguration.Log4netLevel; private EventData ToEventData(LoggingEvent loggingEvent) { var eventData = new EventData { - ProviderName = nameof(Log4netInput), + ProviderName = $"{nameof(Log4netInput)}.{loggingEvent.LoggerName}", Timestamp = loggingEvent.TimeStamp, Level = ToLogLevel[loggingEvent.Level], - Keywords = 0 + Keywords = 0, + Payload = { { "Message", loggingEvent.MessageObject } } }; + if (loggingEvent.ExceptionObject != null) + { + eventData.Payload.Add("Exception", loggingEvent.ExceptionObject); + } + foreach (var key in loggingEvent.Properties.GetKeys()) { try