Skip to content

Commit

Permalink
Merge pull request #6 from CanalSharp/dev
Browse files Browse the repository at this point in the history
Add NLog & Microsoft log support.
  • Loading branch information
stulzq committed Sep 25, 2018
2 parents a9ace25 + 049a70c commit 9227f41
Show file tree
Hide file tree
Showing 29 changed files with 325 additions and 146 deletions.
19 changes: 18 additions & 1 deletion CanalSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CanalSharp.Common", "src\Ca
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "04 sample", "04 sample", "{A4650FA0-4725-4956-90D3-F462F3C7474F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CanalSharp.SimpleClient", "sample\CanalSharp.SimpleClient\CanalSharp.SimpleClient.csproj", "{CE9C0E45-F613-4E28-89EE-7F646B028BFB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CanalSharp.SimpleClient", "sample\CanalSharp.SimpleClient\CanalSharp.SimpleClient.csproj", "{CE9C0E45-F613-4E28-89EE-7F646B028BFB}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "logging", "logging", "{37A77DF6-2765-4834-B0F2-4C58EB19D72C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CanalSharp.Logging.NLog", "src\Logging\CanalSharp.Logging.NLog\CanalSharp.Logging.NLog.csproj", "{4C4796DA-78D7-46E9-B633-F9916EDC94FB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CanalSharp.Logging.Microsoft.Extensions.Logging", "src\Logging\CanalSharp.Logging.Microsoft.Extensions.Logging\CanalSharp.Logging.Microsoft.Extensions.Logging.csproj", "{EBBB57C0-B8CF-4705-863F-3F990D613651}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -54,6 +60,14 @@ Global
{CE9C0E45-F613-4E28-89EE-7F646B028BFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE9C0E45-F613-4E28-89EE-7F646B028BFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE9C0E45-F613-4E28-89EE-7F646B028BFB}.Release|Any CPU.Build.0 = Release|Any CPU
{4C4796DA-78D7-46E9-B633-F9916EDC94FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4C4796DA-78D7-46E9-B633-F9916EDC94FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4C4796DA-78D7-46E9-B633-F9916EDC94FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C4796DA-78D7-46E9-B633-F9916EDC94FB}.Release|Any CPU.Build.0 = Release|Any CPU
{EBBB57C0-B8CF-4705-863F-3F990D613651}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EBBB57C0-B8CF-4705-863F-3F990D613651}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBBB57C0-B8CF-4705-863F-3F990D613651}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EBBB57C0-B8CF-4705-863F-3F990D613651}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -64,6 +78,9 @@ Global
{B9782080-16BD-4328-A394-3EC8D9B49326} = {4B78B79B-7EBE-405E-9BE3-3A59FFAD1910}
{B583684C-4E2F-42A5-8F08-1E3C4EE4C586} = {4B78B79B-7EBE-405E-9BE3-3A59FFAD1910}
{CE9C0E45-F613-4E28-89EE-7F646B028BFB} = {A4650FA0-4725-4956-90D3-F462F3C7474F}
{37A77DF6-2765-4834-B0F2-4C58EB19D72C} = {4B78B79B-7EBE-405E-9BE3-3A59FFAD1910}
{4C4796DA-78D7-46E9-B633-F9916EDC94FB} = {37A77DF6-2765-4834-B0F2-4C58EB19D72C}
{EBBB57C0-B8CF-4705-863F-3F990D613651} = {37A77DF6-2765-4834-B0F2-4C58EB19D72C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FD9B9146-8418-4F80-A666-9B47BE4F9C9C}
Expand Down
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<VersionMajor>0</VersionMajor>
<VersionMinor>1</VersionMinor>
<VersionMinor>2</VersionMinor>
<VersionPatch>0</VersionPatch>
<VersionQuality></VersionQuality>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
Expand Down
7 changes: 7 additions & 0 deletions sample/CanalSharp.SimpleClient/CanalSharp.SimpleClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
<ItemGroup>
<ProjectReference Include="..\..\src\CanalSharp.Client\CanalSharp.Client.csproj" />
<ProjectReference Include="..\..\src\CanalSharp.Protocol\CanalSharp.Protocol.csproj" />
<ProjectReference Include="..\..\src\Logging\CanalSharp.Logging.NLog\CanalSharp.Logging.NLog.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="NLog.Config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions sample/CanalSharp.SimpleClient/NLog.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<targets>
<target name="asyncFile" xsi:type="AsyncWrapper">
<target name="log_file" xsi:type="File"
fileName="${basedir}/Logs/${shortdate}/${logger}-${level}-${shortdate}.txt"
layout="${longdate} | ${message} ${onexception:${exception:format=message} ${newline} ${stacktrace} ${newline}"
archiveFileName="${basedir}/archives/${logger}-${level}-${shortdate}-{#####}.txt"
archiveAboveSize="102400"
archiveNumbering="Sequence"
concurrentWrites="true"
keepFileOpen="false" />
</target>
<target name="console" xsi:type="ColoredConsole" layout="[${date:format=HH\:mm\:ss}]:${message} ${exception:format=message}" />
</targets>

<rules>
<logger name="*" minlevel="Debug" writeTo="asyncFile" />
<logger name="*" minlevel="Debug" writeTo="console" />
</rules>
</nlog>
13 changes: 12 additions & 1 deletion sample/CanalSharp.SimpleClient/Program.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using CanalSharp.Client.Impl;
using CanalSharp.Common.Logging;
using CanalSharp.Logging.NLog;
using Com.Alibaba.Otter.Canal.Protocol;
using NLog;
using NLog.Config;

namespace CanalSharp.SimpleClient
{
class Program
{
static void Main(string[] args)
{
LogManager.Configuration = new XmlLoggingConfiguration("NLog.Config");
var logger = LogManager.GetLogger("test");
logger.Debug("Nlog enabled.");
//设置nlog
CanalSharpLogManager.SetLoggerFactory(new NLogLoggerFactory());

//canal 配置的 destination,默认为 example
var destination = "example";
//创建一个简单CanalClient连接对象(此对象不支持集群)传入参数分别为 canal地址、端口、destination、用户名、密码
Expand All @@ -27,7 +38,7 @@ static void Main(string[] args)
var batchId = message.Id;
if (batchId == -1 || message.Entries.Count <= 0)
{
Console.WriteLine("=====没有数据了=====");
// Console.WriteLine("=====没有数据了=====");
Thread.Sleep(300);
continue;
}
Expand Down
6 changes: 4 additions & 2 deletions src/CanalSharp.Client/Impl/SimpleCanalConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace CanalSharp.Client.Impl
{
public class SimpleCanalConnector : ChannelHandlerAdapter, ICanalConnector
{
private readonly ILogger _logger = LogManager.GetLogger(typeof(SimpleCanalConnector));
private readonly ICanalSharpLogger _logger = CanalSharpLogManager.GetLogger(typeof(SimpleCanalConnector));
public string Address { get; set; }
public int Port { get; set; }
public string Username { get; set; }
Expand Down Expand Up @@ -167,7 +167,7 @@ public void Subscribe(string filter)
{
Destination = _clientIdentity.Destination,
ClientId = _clientIdentity.ClientId.ToString(),
Filter = _filter != null ? _filter : ""
Filter = _filter ?? ""
}.ToByteString()
}.ToByteArray();

Expand All @@ -181,6 +181,7 @@ public void Subscribe(string filter)
}

_clientIdentity.Filter = filter;
_logger.Debug("Subscribe success. Filter: "+ filter);
}
catch (Exception e)
{
Expand Down Expand Up @@ -460,6 +461,7 @@ private void DoConnect()
}

_connected = _tcpClient.Connected;
_logger.Debug($"Canal connect success. IP: {Address}, Port: {Port}");
}
}
catch (Exception e)
Expand Down
6 changes: 1 addition & 5 deletions src/CanalSharp.Common/Alarm/ICanalAlarmHandler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CanalSharp.Common.Alarm
namespace CanalSharp.Common.Alarm
{
/// <summary>
/// canal报警处理机制
Expand Down
7 changes: 2 additions & 5 deletions src/CanalSharp.Common/Alarm/Impl/LogAlarmHandler.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
using CanalSharp.Common.Logging;
using CanalSharp.Common.Logging;

namespace CanalSharp.Common.Alarm.Impl
{
public class LogAlarmHandler : ICanalAlarmHandler
{
private readonly ILogger _logger = LogManager.GetLogger(typeof(LogAlarmHandler));
private readonly ICanalSharpLogger _logger = CanalSharpLogManager.GetLogger(typeof(LogAlarmHandler));
public void SendAlarm(string destination, string msg)
{
_logger.Warning($"destination:{destination}[{msg}]");
Expand Down
26 changes: 26 additions & 0 deletions src/CanalSharp.Common/Logging/CanalSharpLogManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

namespace CanalSharp.Common.Logging
{
public static class CanalSharpLogManager
{
private static readonly ICanalSharpLoggerFactory DefaultLoggerFactory = new CanalSharpNullLoggerFactory();
private static ICanalSharpLoggerFactory _loggerFactory;

public static ICanalSharpLogger GetLogger(Type type)
{
var loggerFactory = _loggerFactory ?? DefaultLoggerFactory;
return loggerFactory.CreateLogger(type);
}

public static ICanalSharpLogger GetLogger<T>()
{
return GetLogger(typeof(T));
}

public static void SetLoggerFactory(ICanalSharpLoggerFactory loggerFactory)
{
_loggerFactory = loggerFactory;
}
}
}
27 changes: 27 additions & 0 deletions src/CanalSharp.Common/Logging/CanalSharpNullLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace CanalSharp.Common.Logging
{
internal class CanalSharpNullLogger : ICanalSharpLogger
{
public void Debug(string message)
{
}

public void Info(string message)
{
}

public void Warning(string message)
{
}

public void Error(string message, System.Exception exception)
{
}

public void Trace(string message)
{
}
}
}
12 changes: 12 additions & 0 deletions src/CanalSharp.Common/Logging/CanalSharpNullLoggerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace CanalSharp.Common.Logging
{
public class CanalSharpNullLoggerFactory : ICanalSharpLoggerFactory
{
public ICanalSharpLogger CreateLogger(Type type)
{
return new CanalSharpNullLogger();
}
}
}
16 changes: 16 additions & 0 deletions src/CanalSharp.Common/Logging/ICanalSharpLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

namespace CanalSharp.Common.Logging
{
public interface ICanalSharpLogger
{
void Debug(string message);

void Info(string message);

void Warning(string message);

void Error(string message, System.Exception exception);

void Trace(string message);
}
}
9 changes: 9 additions & 0 deletions src/CanalSharp.Common/Logging/ICanalSharpLoggerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace CanalSharp.Common.Logging
{
public interface ICanalSharpLoggerFactory
{
ICanalSharpLogger CreateLogger(Type type);
}
}
17 changes: 0 additions & 17 deletions src/CanalSharp.Common/Logging/ILogger.cs

This file was deleted.

9 changes: 0 additions & 9 deletions src/CanalSharp.Common/Logging/ILoggerFactory.cs

This file was deleted.

26 changes: 0 additions & 26 deletions src/CanalSharp.Common/Logging/LogManager.cs

This file was deleted.

27 changes: 0 additions & 27 deletions src/CanalSharp.Common/Logging/NullLogger.cs

This file was deleted.

12 changes: 0 additions & 12 deletions src/CanalSharp.Common/Logging/NullLoggerFactory.cs

This file was deleted.

Loading

0 comments on commit 9227f41

Please # to comment.