Skip to content

Commit

Permalink
add .Net Core support
Browse files Browse the repository at this point in the history
  • Loading branch information
Gorniv committed Aug 17, 2016
1 parent c16cadb commit 6b4bbd5
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -154,3 +154,5 @@ $RECYCLE.BIN/

# Mac desktop service store files
.DS_Store
src/.vs
project.lock.json
39 changes: 39 additions & 0 deletions src/NLog.Targets.Syslog.Dotnet.sln
@@ -0,0 +1,39 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{92015ECC-C6DD-4D66-8A36-6ADF2464984C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{EA7BE9F4-DF35-4AC1-B2B5-1E16A9B78349}"
ProjectSection(SolutionItems) = preProject
global.json = global.json
EndProjectSection
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "NLog.Targets.Syslog.Dotnet", "NLog.Targets.Syslog.Dotnet\NLog.Targets.Syslog.Dotnet.xproj", "{F4B2393A-691E-4EC3-B4C4-C03AE4D32321}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "NLog.Targets.Syslog.Test.Dotnet", "NLog.Targets.Syslog.Test.Dotnet\NLog.Targets.Syslog.Test.Dotnet.xproj", "{A687BAD3-8198-4A15-8246-2D6148815B5F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F4B2393A-691E-4EC3-B4C4-C03AE4D32321}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F4B2393A-691E-4EC3-B4C4-C03AE4D32321}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F4B2393A-691E-4EC3-B4C4-C03AE4D32321}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F4B2393A-691E-4EC3-B4C4-C03AE4D32321}.Release|Any CPU.Build.0 = Release|Any CPU
{A687BAD3-8198-4A15-8246-2D6148815B5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A687BAD3-8198-4A15-8246-2D6148815B5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A687BAD3-8198-4A15-8246-2D6148815B5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A687BAD3-8198-4A15-8246-2D6148815B5F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{F4B2393A-691E-4EC3-B4C4-C03AE4D32321} = {92015ECC-C6DD-4D66-8A36-6ADF2464984C}
{A687BAD3-8198-4A15-8246-2D6148815B5F} = {92015ECC-C6DD-4D66-8A36-6ADF2464984C}
EndGlobalSection
EndGlobal
21 changes: 21 additions & 0 deletions src/NLog.Targets.Syslog.Dotnet/NLog.Targets.Syslog.Dotnet.xproj
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>

<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>f4b2393a-691e-4ec3-b4c4-c03ae4d32321</ProjectGuid>
<RootNamespace>NLog.Targets.Syslog.Dotnet</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
</PropertyGroup>

<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
35 changes: 35 additions & 0 deletions src/NLog.Targets.Syslog.Dotnet/project.json
@@ -0,0 +1,35 @@
{
"version": "2.2.0-*",
"buildOptions": {
"compile": {
"include": [ "../NLog.Targets.Syslog/**/*.cs" ]
},
"warningsAsErrors": false,
"outputName": "NLog.Targets.Syslog",
"xmlDoc": true
},
"configurations": {
"Debug": {
"buildOptions": {
"define": [ "DEBUG", "TRACE" ]
}
},
"Release": {
"buildOptions": {
"define": [ "TRACE" ],
"optimize": true
}
}
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore5.0"

}
},
"dependencies": {
"NETStandard.Library": "1.6.0",
"NLog": "4.4.0-betaV15",
"System.Net.Security": "4.0.0"
}
}
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>a687bad3-8198-4a15-8246-2d6148815b5f</ProjectGuid>
<RootNamespace>NLog.Targets.Syslog.Test.Dotnet</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
48 changes: 48 additions & 0 deletions src/NLog.Targets.Syslog.Test.Dotnet/NLog.config
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
This file needs to be put in the application directory. Make sure to set
'Copy to Output Directory' option in Visual Studio.
-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<extensions>
<add assembly="NLog.Targets.Syslog" />
</extensions>

<variable name="enterpriseId" value="12345"/>

<targets>
<target
name="syslog"
type="Syslog"
syslogserver="127.0.0.1"
port="514"
facility="Local7"
sender="MyProgram"
layout="[CustomPrefix] ${machinename} ${message} ${callsite} ${exception:format=ToString,StackTrace}" />

<target type="AsyncWrapper" name="async">
<target
name="syslog-new"
type="Syslog"
syslogserver="127.0.0.1"
port="514"
facility="Local7"
rfc="Rfc5424"
machineName="${machinename}"
sender="${logger}"
procid="${processid}"
msgid="testmsgid"
structureddata="[thread@${enterpriseId} id=&quot;${threadid}&quot;]"
layout="${message} ${exception:format=ToString,StackTrace}" />
</target>
</targets>

<rules>
<!--<logger name="*" minLevel="Trace" appendTo="syslog"/>-->
<!--<logger name="*" minLevel="Trace" appendTo="syslog-new"/>-->
<logger name="*" minLevel="Trace" appendTo="async"/>
</rules>

</nlog>
19 changes: 19 additions & 0 deletions src/NLog.Targets.Syslog.Test.Dotnet/Properties/AssemblyInfo.cs
@@ -0,0 +1,19 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NLog.Targets.Syslog.Test.Dotnet")]
[assembly: AssemblyTrademark("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("a687bad3-8198-4a15-8246-2d6148815b5f")]
38 changes: 38 additions & 0 deletions src/NLog.Targets.Syslog.Test.Dotnet/TestLogger.cs
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NLog.Config;
using Xunit;
using NLog;
using NLog.Extensions.Logging;

namespace NLog.Targets.Syslog.Test.Dotnet
{
public class TestLogger
{

public TestLogger()
{

}

[Fact]
public void TestLog()
{
ILoggerFactory loggerFactory = new LoggerFactory();
loggerFactory.AddNLog();
var logger = loggerFactory.CreateLogger("TestLogger");
var config = new XmlLoggingConfiguration("Nlog.config");
LogManager.Configuration = config;
Assert.True(config.AllTargets.Count>0);
logger.LogTrace("This is a sample trace message");
logger.LogDebug("This is a sample debug message");
logger.LogInformation("This is a sample info message");
logger.LogWarning("This is a sample warn message");
logger.LogError("This is a sample error message");
logger.LogCritical("This is a sample fatal message");
}
}
}
25 changes: 25 additions & 0 deletions src/NLog.Targets.Syslog.Test.Dotnet/project.json
@@ -0,0 +1,25 @@
{
"version": "1.0.0-*",

"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"Microsoft.Extensions.Logging.Abstractions": "1.0.0",
"NETStandard.Library": "1.6.0",
"NLog": "4.4.0-betaV15",
"xunit": "2.2.0-beta2-build3300",
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"Microsoft.Extensions.Logging": "1.0.0",
"NLog.Extensions.Logging": "1.0.0-*",
"NLog.Targets.Syslog.Dotnet": "2.2.0-*"
},

"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore5.0"
}
},
"testRunner": "xunit"
}
44 changes: 34 additions & 10 deletions src/NLog.Targets.Syslog/NLog.Targets.Syslog.cs
Expand Up @@ -96,8 +96,12 @@ public class Syslog : TargetWithLayout
public Syslog()
{
SyslogServer = "127.0.0.1";
Port = 514;
Port = 514;
#if NET45
Sender = Assembly.GetCallingAssembly().GetName().Name;
#else
Sender = Assembly.GetEntryAssembly().GetName().Name;
#endif
Facility = SyslogFacility.Local1;
Protocol = ProtocolType.Udp;
TimestampFormat = "MMM dd HH:mm:ss";
Expand Down Expand Up @@ -132,32 +136,52 @@ protected override void Write(AsyncLogEventInfo[] logEvents)
/// <summary>Sends array of events to syslog server</summary>
/// <param name="logEvents">The array of NLog.AsyncLogEventInfo</param>
private void SendEventsBatch(params AsyncLogEventInfo[] logEvents)
{
{
#if NET45
var logServerIp = Dns.GetHostAddresses(SyslogServer).FirstOrDefault();
#else
var logServerIp = (Dns.GetHostAddressesAsync(SyslogServer)).Result.FirstOrDefault();
#endif
if (logServerIp == null)
{
return;
}
var ipAddress = logServerIp.ToString();
switch (Protocol)
{
case ProtocolType.Udp:
using (var udp = new UdpClient(ipAddress, Port))
{
ProcessAndSendEvents(logEvents, messageData => udp.Send(messageData, messageData.Length));
case ProtocolType.Udp:
#if NET45
using (var udp = new UdpClient(ipAddress, Port))
#else
using (var udp = new UdpClient(new IPEndPoint(logServerIp, Port)))
#endif
{
#if NET45
ProcessAndSendEvents(logEvents, messageData => udp.Send(messageData, messageData.Length));
#else
ProcessAndSendEvents(logEvents, messageData => udp.SendAsync(messageData, messageData.Length, ipAddress, Port));
#endif
}
break;
case ProtocolType.Tcp:
using (var tcp = new TcpClient(ipAddress, Port))
case ProtocolType.Tcp:
#if NET45
using (var tcp = new TcpClient(ipAddress, Port))
#else
using (var tcp = new TcpClient())
#endif
{
// disposition of tcp also disposes stream
var stream = tcp.GetStream();
if (Ssl)
{
// leave stream open so that we don't double dispose
using (var sslStream = new SslStream(stream, true))
{
sslStream.AuthenticateAsClient(SyslogServer);
{
#if NET45
sslStream.AuthenticateAsClient(SyslogServer);
#else
sslStream.AuthenticateAsClientAsync(SyslogServer);
#endif
ProcessAndSendEvents(logEvents, messageData => sslStream.Write(messageData, 0, messageData.Length));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/NLog.Targets.Syslog/Properties/AssemblyInfo.cs
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.1.*")]
[assembly: AssemblyVersion("2.2.*")]
[assembly: AssemblyFileVersion("2.0.0.0")]
6 changes: 6 additions & 0 deletions src/global.json
@@ -0,0 +1,6 @@
{
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.0-preview2-003121"
}
}

0 comments on commit 6b4bbd5

Please sign in to comment.