Skip to content

Commit

Permalink
Merge pull request #3 from MarkZither/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
MarkZither authored Aug 6, 2020
2 parents fb1bd7c + f9e729d commit 87bc71d
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 58 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ 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)|
|Master Branch|[![Build status](https://ci.appveyor.com/api/projects/status/fh0wk0ov2f86u1lw/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/fh0wk0ov2f86u1lw/branch/master?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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<Configurations>Debug;Release;DebugWithPackageGeneration</Configurations>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.1.0-alpha14</Version>
<SonarQubeExclude>true</SonarQubeExclude>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 4 additions & 1 deletion Samples/Log4net.Appender.InfluxDBSyslog.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ namespace Log4net.Appender.InfluxDBSyslog.ConsoleTest
{
class Program
{
protected Program()
{ }

private static readonly ILog log = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
// 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");
Expand Down
131 changes: 131 additions & 0 deletions Test/Log4net.Appender.InfluxDBSyslog.Tests/InfluxAppenderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using log4net;
using log4net.Config;
using log4net.Layout;
using Log4net.Appender.InfluxDBSyslog;
using Moq;
using RichardSzalay.MockHttp;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Http;
using System.Reflection;
using Xunit;

namespace Log4net.Appender.InfluxDBSyslog.Tests
{
[ExcludeFromCodeCoverage]
public class InfluxAppenderTests : IDisposable
{
private MockRepository mockRepository;

public InfluxAppenderTests()
{
this.mockRepository = new MockRepository(MockBehavior.Strict);
var layout = new PatternLayout("%.255message");
layout.ActivateOptions();

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

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

public void Dispose()
{
this.mockRepository.VerifyAll();
}

private InfluxAppender CreateInfluxAppender()
{
return new InfluxAppender();
}

[Fact]
public void AppendTest()
{
// Arrange
var mockHttp = new MockHttpMessageHandler();

// Setup a respond for the user api (including a wildcard in the URL)
mockHttp.When("http://localhost:8086/*")
.Respond(HttpStatusCode.NoContent, "application/json", "{}"); // Respond with JSON
mockHttp.Fallback.Respond(new HttpClient());
// Inject the handler or client into your application code
var client = mockHttp.ToHttpClient();

Mock<InfluxAppender> mock = new Mock<InfluxAppender>(client) { 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);
}

[Fact]
public void ActivateOptions_StateUnderTest_ExpectedBehavior()
{
// Arrange
var unitUnderTest = this.CreateInfluxAppender();

// Act
unitUnderTest.ActivateOptions();

// Assert
Assert.True(true);
}

[Fact]
public void PropertyRemotePort_StoresCorrectly()
{
// Arrange
var unitUnderTest = this.CreateInfluxAppender();

// Act
unitUnderTest.RemotePort = 8086;

// Assert
Assert.Equal(8086, unitUnderTest.RemotePort);
}

[Fact]
public void PropertyHost_StoresCorrectly()
{
// Arrange
var unitUnderTest = this.CreateInfluxAppender();

// Act
unitUnderTest.Host = "localhost";

// Assert
Assert.Equal("localhost", unitUnderTest.Host);
}

[Fact]
public void PropertyScheme_StoresCorrectly()
{
// Arrange
var unitUnderTest = this.CreateInfluxAppender();

// Act
unitUnderTest.Scheme = "https";

// Assert
Assert.Equal("https", unitUnderTest.Scheme);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

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

public UnitTest1()
public SkippableInfluxAppenderTests()
{
CreateAppender();
}
Expand Down Expand Up @@ -49,41 +49,20 @@ static void CreateAppender()
_log = LogManager.GetLogger(typeof(InfluxAppender));
}

[Fact]
[SkippableFact]
public void AppendIntegrationTest()
{
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);
}

[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));
Expand Down
70 changes: 70 additions & 0 deletions Test/Log4net.Appender.InfluxDBSyslog.Tests/SyslogSeverityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Log4net.Appender.InfluxDBSyslog;
using Moq;
using System;
using Xunit;

namespace Log4net.Appender.InfluxDBSyslog.Tests
{
public class SyslogSeverityTests : IDisposable
{
private MockRepository mockRepository;



public SyslogSeverityTests()
{
this.mockRepository = new MockRepository(MockBehavior.Strict);


}

public void Dispose()
{
this.mockRepository.VerifyAll();
}

private SyslogSeverity CreateSyslogSeverity()
{
return new SyslogSeverity();
}

[Fact]
public void PropertySeverity_StoresCorrectly()
{
// Arrange
var unitUnderTest = this.CreateSyslogSeverity();

// Act
unitUnderTest.Severity = "crit";

// Assert
Assert.Equal("crit", unitUnderTest.Severity);
}

[Fact]
public void PropertySeverityCode_StoresCorrectly()
{
// Arrange
var unitUnderTest = this.CreateSyslogSeverity();

// Act
unitUnderTest.SeverityCode = 1;

// Assert
Assert.Equal(1, unitUnderTest.SeverityCode);
}

[Fact]
public void GetSyslogSeverity_Should_GetSyslogSeverityFromLog4netLevel()
{
// Arrange

// Act
var severity = Log4netSyslogSeverityConvertor.GetSyslogSeverity(log4net.Core.Level.Warn);

// Assert
Assert.Equal("warning", severity.Severity);
Assert.Equal(4, severity.SeverityCode);
}
}
}
26 changes: 3 additions & 23 deletions src/Log4net.Appender.InfluxDBSyslog/InfluxAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public int RemotePort
public string AppName { get; set; }


private HttpClient HttpClient;
private readonly HttpClient HttpClient;
public InfluxAppender()
{
HttpClient = new HttpClient();
Expand All @@ -67,8 +67,7 @@ public InfluxAppender(HttpClient httpClient)

protected override async void Append(LoggingEvent loggingEvent)
{
//var renderedMessage = RenderLoggingEvent(loggingEvent);
SyslogSeverity severity = GetSyslogSeverity(loggingEvent.Level);
SyslogSeverity severity = Log4netSyslogSeverityConvertor.GetSyslogSeverity(loggingEvent.Level);

InfluxDbClientConfiguration config = new InfluxDbClientConfiguration(
new UriBuilder(Scheme, Host, RemotePort).Uri,
Expand Down Expand Up @@ -115,25 +114,6 @@ protected override async void Append(LoggingEvent loggingEvent)
}
}

private SyslogSeverity GetSyslogSeverity(Level level)
{
switch (level.Name)
{
case "FATAL":
return new SyslogSeverity() { Severity = "emerg", SeverityCode = 0 };
case "ERROR":
return new SyslogSeverity() { Severity = "err", SeverityCode = 3 };
case "WARN":
return new SyslogSeverity() { Severity = "warning", SeverityCode = 4 };
case "INFO":
return new SyslogSeverity() { Severity = "info", SeverityCode = 4 };
case "DEBUG":
return new SyslogSeverity() { Severity = "debug", SeverityCode = 6 };
default:
return new SyslogSeverity() { Severity = "notice", SeverityCode = 7 };
}
}

/// <summary>
/// Initialize the appender based on the options set.
/// </summary>
Expand Down Expand Up @@ -162,7 +142,7 @@ public override void ActivateOptions()

if (this.Host == null)
{
throw new ArgumentNullException("RemoteAddress");
throw new ArgumentNullException(this.Host);
}

if (this.RemotePort < IPEndPoint.MinPort || this.RemotePort > IPEndPoint.MaxPort)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using log4net.Core;

namespace Log4net.Appender.InfluxDBSyslog
{
internal static class Log4netSyslogSeverityConvertor
{
internal static SyslogSeverity GetSyslogSeverity(Level level)
{
switch (level.Name)
{
case "FATAL":
return new SyslogSeverity() { Severity = "emerg", SeverityCode = 0 };

case "ERROR":
return new SyslogSeverity() { Severity = "err", SeverityCode = 3 };

case "WARN":
return new SyslogSeverity() { Severity = "warning", SeverityCode = 4 };

case "INFO":
return new SyslogSeverity() { Severity = "info", SeverityCode = 4 };

case "DEBUG":
return new SyslogSeverity() { Severity = "debug", SeverityCode = 6 };

default:
return new SyslogSeverity() { Severity = "notice", SeverityCode = 7 };
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Log4net.Appender.InfluxDBSyslog.Tests")]
Loading

0 comments on commit 87bc71d

Please sign in to comment.