Skip to content

Commit

Permalink
Merge pull request #259 from jeremysmith1/master
Browse files Browse the repository at this point in the history
Log4net input configuration fix and readme updated
  • Loading branch information
karolz-ms authored Aug 19, 2018
2 parents dab2f17 + 9670b15 commit 1ac7094
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 |
Expand Down
21 changes: 15 additions & 6 deletions src/Microsoft.Diagnostics.EventFlow.Inputs.Log4net/Log4netInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down

0 comments on commit 1ac7094

Please sign in to comment.