Skip to content

Commit

Permalink
Merge pull request #2 from MarkZither/dev
Browse files Browse the repository at this point in the history
First working version
  • Loading branch information
MarkZither authored Jun 16, 2019
2 parents 6f80c02 + a5d9ffd commit 97668a3
Show file tree
Hide file tree
Showing 13 changed files with 499 additions and 14 deletions.
8 changes: 8 additions & 0 deletions Log4net.Appender.InfluxDBSyslog.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,32 @@ 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
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
DebugWithPackageGeneration|Any CPU = DebugWithPackageGeneration|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{FFAC394B-DD5C-4220-B780-0DB02CBEF935}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FFAC394B-DD5C-4220-B780-0DB02CBEF935}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FFAC394B-DD5C-4220-B780-0DB02CBEF935}.DebugWithPackageGeneration|Any CPU.ActiveCfg = DebugWithPackageGeneration|Any CPU
{FFAC394B-DD5C-4220-B780-0DB02CBEF935}.DebugWithPackageGeneration|Any CPU.Build.0 = DebugWithPackageGeneration|Any CPU
{FFAC394B-DD5C-4220-B780-0DB02CBEF935}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FFAC394B-DD5C-4220-B780-0DB02CBEF935}.Release|Any CPU.Build.0 = Release|Any CPU
{9D0AB22A-83B3-4513-8A86-B3C270905FDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9D0AB22A-83B3-4513-8A86-B3C270905FDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9D0AB22A-83B3-4513-8A86-B3C270905FDC}.DebugWithPackageGeneration|Any CPU.ActiveCfg = DebugWithPackageGeneration|Any CPU
{9D0AB22A-83B3-4513-8A86-B3C270905FDC}.DebugWithPackageGeneration|Any CPU.Build.0 = DebugWithPackageGeneration|Any CPU
{9D0AB22A-83B3-4513-8A86-B3C270905FDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9D0AB22A-83B3-4513-8A86-B3C270905FDC}.Release|Any CPU.Build.0 = Release|Any CPU
{49B6AB83-E106-4493-86DF-A59B5E77FAB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{49B6AB83-E106-4493-86DF-A59B5E77FAB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{49B6AB83-E106-4493-86DF-A59B5E77FAB3}.DebugWithPackageGeneration|Any CPU.ActiveCfg = DebugWithPackageGeneration|Any CPU
{49B6AB83-E106-4493-86DF-A59B5E77FAB3}.DebugWithPackageGeneration|Any CPU.Build.0 = DebugWithPackageGeneration|Any CPU
{49B6AB83-E106-4493-86DF-A59B5E77FAB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{49B6AB83-E106-4493-86DF-A59B5E77FAB3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
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.
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<Configurations>Debug;Release;DebugWithPackageGeneration</Configurations>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.1.0-alpha14</Version>
</PropertyGroup>

<ItemGroup>
<None Remove="log4net.config" />
</ItemGroup>

<ItemGroup>
<Content Include="log4net.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Log4net.Appender.InfluxDBSyslog\Log4net.Appender.InfluxDBSyslog.csproj" />
</ItemGroup>

</Project>
89 changes: 86 additions & 3 deletions Samples/Log4net.Appender.InfluxDBSyslog.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,95 @@
using System;
using log4net;
using log4net.Config;
using System;
using System.Reflection;
using System.Threading;

namespace Log4net.Appender.InfluxDBSyslog.Console
namespace Log4net.Appender.InfluxDBSyslog.ConsoleTest
{
class Program
{
private static readonly ILog log = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
// Set up a simple configuration that logs on the console.
// Thanks Stackify https://stackify.com/making-log4net-net-core-work/
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
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();
}
}
}
25 changes: 25 additions & 0 deletions Samples/Log4net.Appender.InfluxDBSyslog.Console/log4net.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<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="ConsoleAppender" />
<appender-ref ref="InfluxAppender" />
</root>
</log4net>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@
<TargetFramework>netcoreapp2.1</TargetFramework>

<IsPackable>false</IsPackable>

<Configurations>Debug;Release;DebugWithPackageGeneration</Configurations>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
<PackageReference Include="Moq" Version="4.11.0" />
<PackageReference Include="RichardSzalay.MockHttp" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Xunit.SkippableFact" Version="1.3.12" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Log4net.Appender.InfluxDBSyslog\Log4net.Appender.InfluxDBSyslog.csproj" />
</ItemGroup>

</Project>
85 changes: 84 additions & 1 deletion Test/Log4net.Appender.InfluxDBSyslog.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,96 @@
using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Layout;
using Moq;
using RichardSzalay.MockHttp;
using System;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Threading;
using Xunit;

namespace Log4net.Appender.InfluxDBSyslog.Test
{
public class UnitTest1
{
private static InfluxAppender _appender;
private static ILog _log;

public UnitTest1()
{
CreateAppender();
}

static void CreateAppender()
{
var layout = new PatternLayout("%.255message");
layout.ActivateOptions();

var appender = new InfluxAppender()
{
Name = "InfluxAppender",
Host = "localhost"
};
appender.ActivateOptions();

var diagAppender = new TraceAppender
{
Layout = layout,
Name = "InfluxAppenderDiagLogger",
};
diagAppender.ActivateOptions();

var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
BasicConfigurator.Configure(logRepository, diagAppender);

_appender = appender;
_log = LogManager.GetLogger(typeof(InfluxAppender));
}

[Fact]
public void Test1()
public void AppendIntegrationTest()
{
// Arrange
Mock<InfluxAppender> mock = new Mock<InfluxAppender>() { CallBase = true };
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));
// Act
log.Info("message");

//Assert
Assert.True(true);
}

[SkippableFact]
public void AppendTest()
{
Skip.If(bool.TryParse(Environment.GetEnvironmentVariable("APPVEYOR"), out _));
// Arrange
Mock<InfluxAppender> mock = new Mock<InfluxAppender>() { CallBase = true };
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));
// Act
log.Info("message");

//Assert
Assert.True(true);
}
}
Expand Down
Loading

0 comments on commit 97668a3

Please sign in to comment.