Skip to content

Commit

Permalink
Add configuration to log4net.config
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkZither committed Jun 16, 2019
1 parent 06c844e commit a5d9ffd
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 54 deletions.
1 change: 1 addition & 0 deletions Log4net.Appender.InfluxDBSyslog.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.gitignore = .gitignore
appveyor.yml = appveyor.yml
directory.build.targets = directory.build.targets
README.md = README.md
EndProjectSection
EndProject
Global
Expand Down
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,74 @@
# Log4net.Appender.InfluxDBSyslog
Log4net appender that posts events to InfluxDB in Syslog format

| Branch | Status |
| -------- | -------------- |
|Master Branch|[![Build status](https://ci.appveyor.com/api/projects/status/dvouxggnqka0ptso/branch/master?svg=true)](https://ci.appveyor.com/project/MarkZither/Log4net.Appender.InfluxDBSyslog/branch/master)|
|Dev Branch|[![Build status](https://ci.appveyor.com/api/projects/status/dvouxggnqka0ptso/branch/dev?svg=true)](https://ci.appveyor.com/project/MarkZither/Log4net.Appender.InfluxDBSyslog/branch/dev)|

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=Log4net.Appender.InfluxDBSyslog&metric=alert_status)](https://sonarcloud.io/dashboard?id=Log4net.Appender.InfluxDBSyslog)


## How to setup
### NuGet Package
Install from nuget
```
PM> Install-Package Log4net.Appender.InfluxDBSyslog
```

or
```
dotnet add package Log4net.Appender.InfluxDBSyslog --version 0.1.0-alpha0020
```

### Configure the appender
``` xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="InfluxAppender" type="Log4net.Appender.InfluxDBSyslog.InfluxAppender, Log4net.Appender.InfluxDBSyslog">
<Scheme>http</Scheme>
<Host>localhost</Host>
<RemotePort>8086</RemotePort>
<AppName>Console Test</AppName>
<Facility>Console Test Custom Facility</Facility>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="InfluxAppender" />
</root>
</log4net>
</configuration>
```

### InfluxDB and Chronograf
Download InfluxDB 1.7 from [influxdata.com](https://portal.influxdata.com/downloads/)

Run influxd and chronograf and see your logs flowing in.
![syslog data in Chronograf](docs/img/Chronograf_Syslog_From_Log4net.png)

### Dependencies

- log4net
- InfluxData.net

### Building the project

dotnet build

## Contribute

If you have any idea for an improvement or found a bug, do not hesitate to open an issue.

## Thanks

[Get Your Syslog On](https://www.influxdata.com/blog/get-your-syslog-on/)
[Writing Logs Directly to InfluxDB - DZone](https://dzone.com/articles/writing-logs-directly-to-influxdb)


## License

Log4net.Appenders.Fluentd is distributed under MIT License.
71 changes: 71 additions & 0 deletions Samples/Log4net.Appender.InfluxDBSyslog.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using log4net.Config;
using System;
using System.Reflection;
using System.Threading;

namespace Log4net.Appender.InfluxDBSyslog.ConsoleTest
{
Expand All @@ -16,6 +17,76 @@ static void Main(string[] args)
XmlConfigurator.Configure(logRepository, new System.IO.FileInfo("log4net.config"));
//BasicConfigurator.Configure(logRepository);
Console.WriteLine("Hello World!");
log.Error("Error Console");
log.Debug("Debug Console");
log.Warn("Warn Console");
log.Info("Info Console");
Thread.Sleep(50);
log.Fatal("Fatal Console");
log.Error("Hello Console");
log.Error("Error Console");
log.Debug("Debug Console");
log.Warn("Warn Console");
Thread.Sleep(500);
log.Info("Info Console");
log.Fatal("Fatal Console");
log.Error("Hello Console");
log.Error("Error Console");
log.Debug("Debug Console");
log.Warn("Warn Console");
log.Info("Info Console");
Thread.Sleep(150);
log.Fatal("Fatal Console");
log.Error("Hello Console");
log.Error("Error Console");
log.Debug("Debug Console");
log.Warn("Warn Console");
Thread.Sleep(450);
log.Info("Info Console");
log.Fatal("Fatal Console");
log.Error("Hello Console");
log.Error("Error Console");
log.Debug("Debug Console");
log.Warn("Warn Console");
Thread.Sleep(250);
log.Info("Info Console");
log.Fatal("Fatal Console");
log.Error("Hello Console");
log.Error("Error Console");
log.Debug("Debug Console");
log.Warn("Warn Console");
log.Info("Info Console");
Thread.Sleep(150);
log.Fatal("Fatal Console");
log.Error("Hello Console");
log.Error("Error Console");
log.Debug("Debug Console");
log.Warn("Warn Console");
log.Info("Info Console");
Thread.Sleep(500);
log.Fatal("Fatal Console");
log.Error("Hello Console");
log.Error("Error Console");
log.Debug("Debug Console");
log.Warn("Warn Console");
log.Info("Info Console");
log.Fatal("Fatal Console");
log.Error("Hello Console");
Thread.Sleep(250);
log.Error("Error Console");
log.Debug("Debug Console");
log.Warn("Warn Console");
log.Info("Info Console");
log.Fatal("Fatal Console");
Thread.Sleep(150);
log.Error("Hello Console");
log.Error("Error Console");
log.Debug("Debug Console");
Thread.Sleep(750);
log.Warn("Warn Console");
log.Info("Info Console");
log.Fatal("Fatal Console");
Thread.Sleep(50);
log.Error("Hello Console");
Console.WriteLine("Press any key to exit");
Console.ReadKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
</layout>
</appender>
<appender name="InfluxAppender" type="Log4net.Appender.InfluxDBSyslog.InfluxAppender, Log4net.Appender.InfluxDBSyslog">
<Host>http://localhost:8086</Host>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
<Scheme>http</Scheme>
<Host>localhost</Host>
<RemotePort>8086</RemotePort>
<AppName>Console Test</AppName>
<Facility>Console Test Custom Facility</Facility>
</appender>
<root>
<level value="INFO" />
Expand Down
44 changes: 10 additions & 34 deletions Test/Log4net.Appender.InfluxDBSyslog.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class UnitTest1
{
private static InfluxAppender _appender;
private static ILog _log;
private HttpClient _client;

public UnitTest1()
{
Expand All @@ -32,7 +31,7 @@ static void CreateAppender()
var appender = new InfluxAppender()
{
Name = "InfluxAppender",
Host = "http://localhost:8086"
Host = "localhost"
};
appender.ActivateOptions();

Expand All @@ -53,26 +52,15 @@ static void CreateAppender()
[Fact]
public void AppendIntegrationTest()
{
// Arrange
var callbackExecuted = false;
var json = @"{
""data"": [
{}";

// Arrange
Mock<InfluxAppender> mock = new Mock<InfluxAppender>() { CallBase = true };
mock.Object.Host = "http://localhost:8086";

Action<HttpRequestMessage, CancellationToken> callback = new Action<HttpRequestMessage, CancellationToken>((requestMessage, cancellationToken) => {
Assert.Equal("http://localhost:5000/identity/api/users", requestMessage.RequestUri.AbsoluteUri);
callbackExecuted = true;
});

//var httpMessageHandlerMock = MockHttpMessageHandler(HttpStatusCode.OK, json, callback);
//var httpClient = new Func<HttpClient>(() => new HttpClient(httpMessageHandlerMock.Object));
mock.Object.Host = "localhost";
mock.Object.RemotePort = 8086;
mock.Object.Facility = "App";
mock.Object.AppName = "MyTestApp";

var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());


BasicConfigurator.Configure(logRepository, mock.Object);

var log = LogManager.GetLogger(typeof(InfluxAppender));
Expand All @@ -88,26 +76,14 @@ public void AppendTest()
{
Skip.If(bool.TryParse(Environment.GetEnvironmentVariable("APPVEYOR"), out _));
// Arrange
var callbackExecuted = false;
var json = @"{
""data"": [
{}";


Mock<InfluxAppender> mock = new Mock<InfluxAppender>() { CallBase = true };
mock.Object.Host = "http://localhost:8086";

Action<HttpRequestMessage, CancellationToken> callback = new Action<HttpRequestMessage, CancellationToken>((requestMessage, cancellationToken) => {
Assert.Equal("http://localhost:5000/identity/api/users", requestMessage.RequestUri.AbsoluteUri);
callbackExecuted = true;
});

//var httpMessageHandlerMock = MockHttpMessageHandler(HttpStatusCode.OK, json, callback);
//var httpClient = new Func<HttpClient>(() => new HttpClient(httpMessageHandlerMock.Object));
mock.Object.Host = "localhost";
mock.Object.RemotePort = 8086;
mock.Object.Facility = "App";
mock.Object.AppName = "MyTestApp";

var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());


BasicConfigurator.Configure(logRepository, mock.Object);

var log = LogManager.GetLogger(typeof(InfluxAppender));
Expand Down
Binary file added docs/img/Chronograf_Syslog_From_Log4net.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit a5d9ffd

Please sign in to comment.