Skip to content

Commit 9227f41

Browse files
authored
Merge pull request #6 from CanalSharp/dev
Add NLog & Microsoft log support.
2 parents a9ace25 + 049a70c commit 9227f41

29 files changed

+325
-146
lines changed

CanalSharp.sln

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CanalSharp.Common", "src\Ca
2626
EndProject
2727
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "04 sample", "04 sample", "{A4650FA0-4725-4956-90D3-F462F3C7474F}"
2828
EndProject
29-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CanalSharp.SimpleClient", "sample\CanalSharp.SimpleClient\CanalSharp.SimpleClient.csproj", "{CE9C0E45-F613-4E28-89EE-7F646B028BFB}"
29+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CanalSharp.SimpleClient", "sample\CanalSharp.SimpleClient\CanalSharp.SimpleClient.csproj", "{CE9C0E45-F613-4E28-89EE-7F646B028BFB}"
30+
EndProject
31+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "logging", "logging", "{37A77DF6-2765-4834-B0F2-4C58EB19D72C}"
32+
EndProject
33+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CanalSharp.Logging.NLog", "src\Logging\CanalSharp.Logging.NLog\CanalSharp.Logging.NLog.csproj", "{4C4796DA-78D7-46E9-B633-F9916EDC94FB}"
34+
EndProject
35+
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}"
3036
EndProject
3137
Global
3238
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -54,6 +60,14 @@ Global
5460
{CE9C0E45-F613-4E28-89EE-7F646B028BFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
5561
{CE9C0E45-F613-4E28-89EE-7F646B028BFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
5662
{CE9C0E45-F613-4E28-89EE-7F646B028BFB}.Release|Any CPU.Build.0 = Release|Any CPU
63+
{4C4796DA-78D7-46E9-B633-F9916EDC94FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
64+
{4C4796DA-78D7-46E9-B633-F9916EDC94FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
65+
{4C4796DA-78D7-46E9-B633-F9916EDC94FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
66+
{4C4796DA-78D7-46E9-B633-F9916EDC94FB}.Release|Any CPU.Build.0 = Release|Any CPU
67+
{EBBB57C0-B8CF-4705-863F-3F990D613651}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
68+
{EBBB57C0-B8CF-4705-863F-3F990D613651}.Debug|Any CPU.Build.0 = Debug|Any CPU
69+
{EBBB57C0-B8CF-4705-863F-3F990D613651}.Release|Any CPU.ActiveCfg = Release|Any CPU
70+
{EBBB57C0-B8CF-4705-863F-3F990D613651}.Release|Any CPU.Build.0 = Release|Any CPU
5771
EndGlobalSection
5872
GlobalSection(SolutionProperties) = preSolution
5973
HideSolutionNode = FALSE
@@ -64,6 +78,9 @@ Global
6478
{B9782080-16BD-4328-A394-3EC8D9B49326} = {4B78B79B-7EBE-405E-9BE3-3A59FFAD1910}
6579
{B583684C-4E2F-42A5-8F08-1E3C4EE4C586} = {4B78B79B-7EBE-405E-9BE3-3A59FFAD1910}
6680
{CE9C0E45-F613-4E28-89EE-7F646B028BFB} = {A4650FA0-4725-4956-90D3-F462F3C7474F}
81+
{37A77DF6-2765-4834-B0F2-4C58EB19D72C} = {4B78B79B-7EBE-405E-9BE3-3A59FFAD1910}
82+
{4C4796DA-78D7-46E9-B633-F9916EDC94FB} = {37A77DF6-2765-4834-B0F2-4C58EB19D72C}
83+
{EBBB57C0-B8CF-4705-863F-3F990D613651} = {37A77DF6-2765-4834-B0F2-4C58EB19D72C}
6784
EndGlobalSection
6885
GlobalSection(ExtensibilityGlobals) = postSolution
6986
SolutionGuid = {FD9B9146-8418-4F80-A666-9B47BE4F9C9C}

build/version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup>
33
<VersionMajor>0</VersionMajor>
4-
<VersionMinor>1</VersionMinor>
4+
<VersionMinor>2</VersionMinor>
55
<VersionPatch>0</VersionPatch>
66
<VersionQuality></VersionQuality>
77
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>

sample/CanalSharp.SimpleClient/CanalSharp.SimpleClient.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
<ItemGroup>
99
<ProjectReference Include="..\..\src\CanalSharp.Client\CanalSharp.Client.csproj" />
1010
<ProjectReference Include="..\..\src\CanalSharp.Protocol\CanalSharp.Protocol.csproj" />
11+
<ProjectReference Include="..\..\src\Logging\CanalSharp.Logging.NLog\CanalSharp.Logging.NLog.csproj" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Update="NLog.Config">
16+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
17+
</None>
1118
</ItemGroup>
1219

1320
</Project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
5+
<targets>
6+
<target name="asyncFile" xsi:type="AsyncWrapper">
7+
<target name="log_file" xsi:type="File"
8+
fileName="${basedir}/Logs/${shortdate}/${logger}-${level}-${shortdate}.txt"
9+
layout="${longdate} | ${message} ${onexception:${exception:format=message} ${newline} ${stacktrace} ${newline}"
10+
archiveFileName="${basedir}/archives/${logger}-${level}-${shortdate}-{#####}.txt"
11+
archiveAboveSize="102400"
12+
archiveNumbering="Sequence"
13+
concurrentWrites="true"
14+
keepFileOpen="false" />
15+
</target>
16+
<target name="console" xsi:type="ColoredConsole" layout="[${date:format=HH\:mm\:ss}]:${message} ${exception:format=message}" />
17+
</targets>
18+
19+
<rules>
20+
<logger name="*" minlevel="Debug" writeTo="asyncFile" />
21+
<logger name="*" minlevel="Debug" writeTo="console" />
22+
</rules>
23+
</nlog>

sample/CanalSharp.SimpleClient/Program.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Threading;
56
using CanalSharp.Client.Impl;
7+
using CanalSharp.Common.Logging;
8+
using CanalSharp.Logging.NLog;
69
using Com.Alibaba.Otter.Canal.Protocol;
10+
using NLog;
11+
using NLog.Config;
712

813
namespace CanalSharp.SimpleClient
914
{
1015
class Program
1116
{
1217
static void Main(string[] args)
1318
{
19+
LogManager.Configuration = new XmlLoggingConfiguration("NLog.Config");
20+
var logger = LogManager.GetLogger("test");
21+
logger.Debug("Nlog enabled.");
22+
//设置nlog
23+
CanalSharpLogManager.SetLoggerFactory(new NLogLoggerFactory());
24+
1425
//canal 配置的 destination,默认为 example
1526
var destination = "example";
1627
//创建一个简单CanalClient连接对象(此对象不支持集群)传入参数分别为 canal地址、端口、destination、用户名、密码
@@ -27,7 +38,7 @@ static void Main(string[] args)
2738
var batchId = message.Id;
2839
if (batchId == -1 || message.Entries.Count <= 0)
2940
{
30-
Console.WriteLine("=====没有数据了=====");
41+
// Console.WriteLine("=====没有数据了=====");
3142
Thread.Sleep(300);
3243
continue;
3344
}

src/CanalSharp.Client/Impl/SimpleCanalConnector.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace CanalSharp.Client.Impl
1616
{
1717
public class SimpleCanalConnector : ChannelHandlerAdapter, ICanalConnector
1818
{
19-
private readonly ILogger _logger = LogManager.GetLogger(typeof(SimpleCanalConnector));
19+
private readonly ICanalSharpLogger _logger = CanalSharpLogManager.GetLogger(typeof(SimpleCanalConnector));
2020
public string Address { get; set; }
2121
public int Port { get; set; }
2222
public string Username { get; set; }
@@ -167,7 +167,7 @@ public void Subscribe(string filter)
167167
{
168168
Destination = _clientIdentity.Destination,
169169
ClientId = _clientIdentity.ClientId.ToString(),
170-
Filter = _filter != null ? _filter : ""
170+
Filter = _filter ?? ""
171171
}.ToByteString()
172172
}.ToByteArray();
173173

@@ -181,6 +181,7 @@ public void Subscribe(string filter)
181181
}
182182

183183
_clientIdentity.Filter = filter;
184+
_logger.Debug("Subscribe success. Filter: "+ filter);
184185
}
185186
catch (Exception e)
186187
{
@@ -460,6 +461,7 @@ private void DoConnect()
460461
}
461462

462463
_connected = _tcpClient.Connected;
464+
_logger.Debug($"Canal connect success. IP: {Address}, Port: {Port}");
463465
}
464466
}
465467
catch (Exception e)

src/CanalSharp.Common/Alarm/ICanalAlarmHandler.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
5-
namespace CanalSharp.Common.Alarm
1+
namespace CanalSharp.Common.Alarm
62
{
73
/// <summary>
84
/// canal报警处理机制

src/CanalSharp.Common/Alarm/Impl/LogAlarmHandler.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
using CanalSharp.Common.Logging;
1+
using CanalSharp.Common.Logging;
52

63
namespace CanalSharp.Common.Alarm.Impl
74
{
85
public class LogAlarmHandler : ICanalAlarmHandler
96
{
10-
private readonly ILogger _logger = LogManager.GetLogger(typeof(LogAlarmHandler));
7+
private readonly ICanalSharpLogger _logger = CanalSharpLogManager.GetLogger(typeof(LogAlarmHandler));
118
public void SendAlarm(string destination, string msg)
129
{
1310
_logger.Warning($"destination:{destination}[{msg}]");
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
3+
namespace CanalSharp.Common.Logging
4+
{
5+
public static class CanalSharpLogManager
6+
{
7+
private static readonly ICanalSharpLoggerFactory DefaultLoggerFactory = new CanalSharpNullLoggerFactory();
8+
private static ICanalSharpLoggerFactory _loggerFactory;
9+
10+
public static ICanalSharpLogger GetLogger(Type type)
11+
{
12+
var loggerFactory = _loggerFactory ?? DefaultLoggerFactory;
13+
return loggerFactory.CreateLogger(type);
14+
}
15+
16+
public static ICanalSharpLogger GetLogger<T>()
17+
{
18+
return GetLogger(typeof(T));
19+
}
20+
21+
public static void SetLoggerFactory(ICanalSharpLoggerFactory loggerFactory)
22+
{
23+
_loggerFactory = loggerFactory;
24+
}
25+
}
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
namespace CanalSharp.Common.Logging
4+
{
5+
internal class CanalSharpNullLogger : ICanalSharpLogger
6+
{
7+
public void Debug(string message)
8+
{
9+
}
10+
11+
public void Info(string message)
12+
{
13+
}
14+
15+
public void Warning(string message)
16+
{
17+
}
18+
19+
public void Error(string message, System.Exception exception)
20+
{
21+
}
22+
23+
public void Trace(string message)
24+
{
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)