diff --git a/CanalSharp.sln b/CanalSharp.sln index 71e205c..5c8228a 100644 --- a/CanalSharp.sln +++ b/CanalSharp.sln @@ -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 @@ -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 @@ -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} diff --git a/build/version.props b/build/version.props index 3e9fc0c..c3daa54 100644 --- a/build/version.props +++ b/build/version.props @@ -1,7 +1,7 @@ 0 - 1 + 2 0 $(VersionMajor).$(VersionMinor).$(VersionPatch) diff --git a/sample/CanalSharp.SimpleClient/CanalSharp.SimpleClient.csproj b/sample/CanalSharp.SimpleClient/CanalSharp.SimpleClient.csproj index f2d4469..3a82314 100644 --- a/sample/CanalSharp.SimpleClient/CanalSharp.SimpleClient.csproj +++ b/sample/CanalSharp.SimpleClient/CanalSharp.SimpleClient.csproj @@ -8,6 +8,13 @@ + + + + + + PreserveNewest + diff --git a/sample/CanalSharp.SimpleClient/NLog.Config b/sample/CanalSharp.SimpleClient/NLog.Config new file mode 100644 index 0000000..06cccf5 --- /dev/null +++ b/sample/CanalSharp.SimpleClient/NLog.Config @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sample/CanalSharp.SimpleClient/Program.cs b/sample/CanalSharp.SimpleClient/Program.cs index f835abf..35d3dae 100644 --- a/sample/CanalSharp.SimpleClient/Program.cs +++ b/sample/CanalSharp.SimpleClient/Program.cs @@ -1,9 +1,14 @@ 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 { @@ -11,6 +16,12 @@ 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、用户名、密码 @@ -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; } diff --git a/src/CanalSharp.Client/Impl/SimpleCanalConnector.cs b/src/CanalSharp.Client/Impl/SimpleCanalConnector.cs index 9cc1589..95e0492 100644 --- a/src/CanalSharp.Client/Impl/SimpleCanalConnector.cs +++ b/src/CanalSharp.Client/Impl/SimpleCanalConnector.cs @@ -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; } @@ -167,7 +167,7 @@ public void Subscribe(string filter) { Destination = _clientIdentity.Destination, ClientId = _clientIdentity.ClientId.ToString(), - Filter = _filter != null ? _filter : "" + Filter = _filter ?? "" }.ToByteString() }.ToByteArray(); @@ -181,6 +181,7 @@ public void Subscribe(string filter) } _clientIdentity.Filter = filter; + _logger.Debug("Subscribe success. Filter: "+ filter); } catch (Exception e) { @@ -460,6 +461,7 @@ private void DoConnect() } _connected = _tcpClient.Connected; + _logger.Debug($"Canal connect success. IP: {Address}, Port: {Port}"); } } catch (Exception e) diff --git a/src/CanalSharp.Common/Alarm/ICanalAlarmHandler.cs b/src/CanalSharp.Common/Alarm/ICanalAlarmHandler.cs index a2fe73b..00fc9c6 100644 --- a/src/CanalSharp.Common/Alarm/ICanalAlarmHandler.cs +++ b/src/CanalSharp.Common/Alarm/ICanalAlarmHandler.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace CanalSharp.Common.Alarm +namespace CanalSharp.Common.Alarm { /// /// canal报警处理机制 diff --git a/src/CanalSharp.Common/Alarm/Impl/LogAlarmHandler.cs b/src/CanalSharp.Common/Alarm/Impl/LogAlarmHandler.cs index af9b81a..cbfc3f5 100644 --- a/src/CanalSharp.Common/Alarm/Impl/LogAlarmHandler.cs +++ b/src/CanalSharp.Common/Alarm/Impl/LogAlarmHandler.cs @@ -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}]"); diff --git a/src/CanalSharp.Common/Logging/CanalSharpLogManager.cs b/src/CanalSharp.Common/Logging/CanalSharpLogManager.cs new file mode 100644 index 0000000..823a2e6 --- /dev/null +++ b/src/CanalSharp.Common/Logging/CanalSharpLogManager.cs @@ -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() + { + return GetLogger(typeof(T)); + } + + public static void SetLoggerFactory(ICanalSharpLoggerFactory loggerFactory) + { + _loggerFactory = loggerFactory; + } + } +} \ No newline at end of file diff --git a/src/CanalSharp.Common/Logging/CanalSharpNullLogger.cs b/src/CanalSharp.Common/Logging/CanalSharpNullLogger.cs new file mode 100644 index 0000000..52068d7 --- /dev/null +++ b/src/CanalSharp.Common/Logging/CanalSharpNullLogger.cs @@ -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) + { + } + } +} \ No newline at end of file diff --git a/src/CanalSharp.Common/Logging/CanalSharpNullLoggerFactory.cs b/src/CanalSharp.Common/Logging/CanalSharpNullLoggerFactory.cs new file mode 100644 index 0000000..30ca482 --- /dev/null +++ b/src/CanalSharp.Common/Logging/CanalSharpNullLoggerFactory.cs @@ -0,0 +1,12 @@ +using System; + +namespace CanalSharp.Common.Logging +{ + public class CanalSharpNullLoggerFactory : ICanalSharpLoggerFactory + { + public ICanalSharpLogger CreateLogger(Type type) + { + return new CanalSharpNullLogger(); + } + } +} \ No newline at end of file diff --git a/src/CanalSharp.Common/Logging/ICanalSharpLogger.cs b/src/CanalSharp.Common/Logging/ICanalSharpLogger.cs new file mode 100644 index 0000000..662868d --- /dev/null +++ b/src/CanalSharp.Common/Logging/ICanalSharpLogger.cs @@ -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); + } +} \ No newline at end of file diff --git a/src/CanalSharp.Common/Logging/ICanalSharpLoggerFactory.cs b/src/CanalSharp.Common/Logging/ICanalSharpLoggerFactory.cs new file mode 100644 index 0000000..64b735f --- /dev/null +++ b/src/CanalSharp.Common/Logging/ICanalSharpLoggerFactory.cs @@ -0,0 +1,9 @@ +using System; + +namespace CanalSharp.Common.Logging +{ + public interface ICanalSharpLoggerFactory + { + ICanalSharpLogger CreateLogger(Type type); + } +} \ No newline at end of file diff --git a/src/CanalSharp.Common/Logging/ILogger.cs b/src/CanalSharp.Common/Logging/ILogger.cs deleted file mode 100644 index b0e2add..0000000 --- a/src/CanalSharp.Common/Logging/ILogger.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; - -namespace CanalSharp.Common.Logging -{ - public interface ILogger - { - void Debug(String message); - - void Info(String message); - - void Warning(String message); - - void Error(String message, System.Exception exception); - - void Trace(String message); - } -} \ No newline at end of file diff --git a/src/CanalSharp.Common/Logging/ILoggerFactory.cs b/src/CanalSharp.Common/Logging/ILoggerFactory.cs deleted file mode 100644 index 7b9c938..0000000 --- a/src/CanalSharp.Common/Logging/ILoggerFactory.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace CanalSharp.Common.Logging -{ - public interface ILoggerFactory - { - ILogger CreateLogger(Type type); - } -} \ No newline at end of file diff --git a/src/CanalSharp.Common/Logging/LogManager.cs b/src/CanalSharp.Common/Logging/LogManager.cs deleted file mode 100644 index 2e03f16..0000000 --- a/src/CanalSharp.Common/Logging/LogManager.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; - -namespace CanalSharp.Common.Logging -{ - public static class LogManager - { - private static readonly ILoggerFactory defaultLoggerFactory = new NullLoggerFactory(); - private static ILoggerFactory _loggerFactory; - - public static ILogger GetLogger(Type type) - { - var loggerFactory = _loggerFactory ?? defaultLoggerFactory; - return loggerFactory.CreateLogger(type); - } - - public static ILogger GetLogger() - { - return GetLogger(typeof(T)); - } - - public static void SetLoggerFactory(ILoggerFactory loggerFactory) - { - _loggerFactory = loggerFactory; - } - } -} \ No newline at end of file diff --git a/src/CanalSharp.Common/Logging/NullLogger.cs b/src/CanalSharp.Common/Logging/NullLogger.cs deleted file mode 100644 index 80627ad..0000000 --- a/src/CanalSharp.Common/Logging/NullLogger.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace CanalSharp.Common.Logging -{ - internal class NullLogger : ILogger - { - 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) - { - } - } -} \ No newline at end of file diff --git a/src/CanalSharp.Common/Logging/NullLoggerFactory.cs b/src/CanalSharp.Common/Logging/NullLoggerFactory.cs deleted file mode 100644 index 33cb92b..0000000 --- a/src/CanalSharp.Common/Logging/NullLoggerFactory.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace CanalSharp.Common.Logging -{ - public class NullLoggerFactory : ILoggerFactory - { - public ILogger CreateLogger(Type type) - { - return new NullLogger(); - } - } -} \ No newline at end of file diff --git a/src/CanalSharp.Common/Utils/AddressUtils.cs b/src/CanalSharp.Common/Utils/AddressUtils.cs index f0fd868..941b306 100644 --- a/src/CanalSharp.Common/Utils/AddressUtils.cs +++ b/src/CanalSharp.Common/Utils/AddressUtils.cs @@ -12,10 +12,10 @@ namespace CanalSharp.Common.Utils public class AddressUtils { - private static readonly ILogger _logger = LogManager.GetLogger(typeof(AddressUtils)); + private static readonly ICanalSharpLogger Logger = CanalSharpLogManager.GetLogger(typeof(AddressUtils)); private static string LOCALHOST_IP = "127.0.0.1"; private static string EMPTY_IP = "0.0.0.0"; - private static readonly Regex IP_PATTERN = new Regex("[0-9]{1,3}(\\.[0-9]{1,3}){3,}"); + private static readonly Regex IpPattern = new Regex("[0-9]{1,3}(\\.[0-9]{1,3}){3,}"); public static bool IsAvailablePort(int port) @@ -23,7 +23,7 @@ public static bool IsAvailablePort(int port) TcpListener ss = null; try { - ss = new TcpListener(port); + ss = new TcpListener(IPAddress.Any,port); ss.Start(); return true; } @@ -41,6 +41,7 @@ public static bool IsAvailablePort(int port) } catch (IOException e) { + } } } @@ -50,7 +51,7 @@ private static bool IsValidHostAddress(IPAddress address) { if (address == null || IPAddress.IsLoopback(address)) return false; var name = Dns.GetHostEntry(address).HostName; - return (name != null && !EMPTY_IP.Equals(name) && !LOCALHOST_IP.Equals(name) && IP_PATTERN.IsMatch(name)); + return (name != null && !EMPTY_IP.Equals(name) && !LOCALHOST_IP.Equals(name) && IpPattern.IsMatch(name)); } @@ -95,36 +96,31 @@ public static IPAddress GetHostAddress() } catch (System.Exception e) { - _logger.Warning($"Failed to retriving local host ip address, try scan network card ip address. cause: {e.Message}"); + Logger.Warning($"Failed to retriving local host ip address, try scan network card ip address. cause: {e.Message}"); } try { - var interfaces = NetworkInterface.GetAllNetworkInterfaces(); - if (interfaces != null) + foreach (var ni in NetworkInterface.GetAllNetworkInterfaces()) { - foreach (var ni in NetworkInterface.GetAllNetworkInterfaces()) + if (ni.NetworkInterfaceType != NetworkInterfaceType.Wireless80211 && + ni.NetworkInterfaceType != NetworkInterfaceType.Ethernet) continue; + Console.WriteLine(ni.Name); + foreach (var ip in ni.GetIPProperties().UnicastAddresses) { - if (ni.NetworkInterfaceType != NetworkInterfaceType.Wireless80211 && - ni.NetworkInterfaceType != NetworkInterfaceType.Ethernet) continue; - Console.WriteLine(ni.Name); - foreach (var ip in ni.GetIPProperties().UnicastAddresses) + if (ip.Address.AddressFamily != AddressFamily.InterNetwork) continue; + var address = ip.Address; + if (IsValidHostAddress(address)) { - if (ip.Address.AddressFamily != AddressFamily.InterNetwork) continue; - var address = ip.Address; - if (IsValidHostAddress(address)) - { - return address; - } + return address; } } - } } catch (System.Exception e) { - _logger.Warning($"Failed to retriving network card ip address. cause:{e.Message}"); + Logger.Warning($"Failed to retriving network card ip address. cause:{e.Message}"); } - _logger.Warning("Could not get local host ip address, will use 127.0.0.1 instead."); + Logger.Warning("Could not get local host ip address, will use 127.0.0.1 instead."); return localAddress; } diff --git a/src/CanalSharp.Protocol/Position/EntryPosition.cs b/src/CanalSharp.Protocol/Position/EntryPosition.cs index ff94a5b..a717220 100644 --- a/src/CanalSharp.Protocol/Position/EntryPosition.cs +++ b/src/CanalSharp.Protocol/Position/EntryPosition.cs @@ -12,7 +12,9 @@ public class EntryPosition:TimePosition public string JournalName { get; set; } public long? Position { get; set; } - //// 记录一下位点对应的serverId + /// + /// 记录一下位点对应的serverId + /// public long? ServerId { get; set; } public string Gtid = null; @@ -104,20 +106,14 @@ public override bool Equals(object obj) return true; } - /** - * {@inheritDoc} - * - * @see java.lang.Comparable#compareTo(java.lang.Object) - */ + public int CompareTo(EntryPosition o) { var val = string.Compare(JournalName, o.JournalName, StringComparison.Ordinal); if (val != 0) return val; - if (Position != null) - { - return (int) (Position - o.Position); - } + if (Position == null) return val; + if (o.Position != null) return (int) (Position - o.Position); return val; } } diff --git a/src/CanalSharp.Protocol/Position/LogIdentity.cs b/src/CanalSharp.Protocol/Position/LogIdentity.cs index bfaf960..bc0b5a7 100644 --- a/src/CanalSharp.Protocol/Position/LogIdentity.cs +++ b/src/CanalSharp.Protocol/Position/LogIdentity.cs @@ -5,9 +5,9 @@ namespace CanalSharp.Protocol.Position public class LogIdentity:Position { // 链接服务器的地址 - public SocketAddress SourceAddress { get; set; } + public SocketAddress SourceAddress { get; } // 对应的slaveId - public long? SlaveId { get; set; } + public long? SlaveId { get; } public LogIdentity() { diff --git a/src/CanalSharp.Protocol/Position/LogPosition.cs b/src/CanalSharp.Protocol/Position/LogPosition.cs index 1ab3825..b9bd04e 100644 --- a/src/CanalSharp.Protocol/Position/LogPosition.cs +++ b/src/CanalSharp.Protocol/Position/LogPosition.cs @@ -2,8 +2,8 @@ { public class LogPosition: Position { - public LogIdentity Identity { get; set; } - private EntryPosition Postion { get; set; } + public LogIdentity Identity { get; } + private EntryPosition Postion { get; } public override int GetHashCode() { diff --git a/src/CanalSharp.Protocol/Position/Position.cs b/src/CanalSharp.Protocol/Position/Position.cs index 520d553..3d795bc 100644 --- a/src/CanalSharp.Protocol/Position/Position.cs +++ b/src/CanalSharp.Protocol/Position/Position.cs @@ -2,9 +2,7 @@ namespace CanalSharp.Protocol.Position { - /** - * 事件唯一标示 - */ + [Serializable] public abstract class Position { diff --git a/src/CanalSharp.Protocol/Position/PositionRange.cs b/src/CanalSharp.Protocol/Position/PositionRange.cs index cce3332..16a9723 100644 --- a/src/CanalSharp.Protocol/Position/PositionRange.cs +++ b/src/CanalSharp.Protocol/Position/PositionRange.cs @@ -6,7 +6,9 @@ namespace CanalSharp.Protocol.Position public class PositionRange where T:Position { public T Start { get; set; } - // add by ljh at 2012-09-05,用于记录一个可被ack的位置,保证每次提交到cursor中的位置是一个完整事务的结束 + /// + /// add by ljh at 2012-09-05,用于记录一个可被ack的位置,保证每次提交到cursor中的位置是一个完整事务的结束 + /// public T Ack { get; set; } public T End { get; set; } diff --git a/src/CanalSharp.Protocol/Position/TimePosition.cs b/src/CanalSharp.Protocol/Position/TimePosition.cs index 0ea62c7..84e73c5 100644 --- a/src/CanalSharp.Protocol/Position/TimePosition.cs +++ b/src/CanalSharp.Protocol/Position/TimePosition.cs @@ -2,7 +2,7 @@ { public class TimePosition: Position { - protected long? Timestamp { get; set; } + protected long? Timestamp { get; } public TimePosition(long? timestamp) { diff --git a/src/Logging/CanalSharp.Logging.Microsoft.Extensions.Logging/CanalSharp.Logging.Microsoft.Extensions.Logging.csproj b/src/Logging/CanalSharp.Logging.Microsoft.Extensions.Logging/CanalSharp.Logging.Microsoft.Extensions.Logging.csproj new file mode 100644 index 0000000..4322aba --- /dev/null +++ b/src/Logging/CanalSharp.Logging.Microsoft.Extensions.Logging/CanalSharp.Logging.Microsoft.Extensions.Logging.csproj @@ -0,0 +1,16 @@ + + + + netstandard2.0 + CanalSharp Microsoft Log日志支持 + + + + + + + + + + + diff --git a/src/Logging/CanalSharp.Logging.Microsoft.Extensions.Logging/MicrosoftExtensionsLoggingLoggerFactory.cs b/src/Logging/CanalSharp.Logging.Microsoft.Extensions.Logging/MicrosoftExtensionsLoggingLoggerFactory.cs new file mode 100644 index 0000000..2e0b9e6 --- /dev/null +++ b/src/Logging/CanalSharp.Logging.Microsoft.Extensions.Logging/MicrosoftExtensionsLoggingLoggerFactory.cs @@ -0,0 +1,54 @@ +using System; +using CanalSharp.Common.Logging; +using Microsoft.Extensions.Logging; + +namespace CanalSharp.Logging.Microsoft.Extensions.Logging +{ + public class MicrosoftExtensionsLoggingLoggerFactory:ICanalSharpLoggerFactory + { + private readonly ILoggerFactory _loggerFactory; + + public MicrosoftExtensionsLoggingLoggerFactory(ILoggerFactory loggerFactory) + { + _loggerFactory = loggerFactory; + } + public ICanalSharpLogger CreateLogger(Type type) + { + return new MicrosoftExtensionsLoggingLogger(_loggerFactory.CreateLogger(type)); + } + + internal class MicrosoftExtensionsLoggingLogger:ICanalSharpLogger + { + private readonly ILogger _logger; + + public MicrosoftExtensionsLoggingLogger(ILogger logger) + { + _logger = logger; + } + public void Debug(string message) + { + _logger.LogDebug(message); + } + + public void Info(string message) + { + _logger.LogInformation(message); + } + + public void Warning(string message) + { + _logger.LogWarning(message); + } + + public void Error(string message, Exception exception) + { + _logger.LogError(exception,message); + } + + public void Trace(string message) + { + _logger.LogTrace(message); + } + } + } +} diff --git a/src/Logging/CanalSharp.Logging.NLog/CanalSharp.Logging.NLog.csproj b/src/Logging/CanalSharp.Logging.NLog/CanalSharp.Logging.NLog.csproj new file mode 100644 index 0000000..45b964b --- /dev/null +++ b/src/Logging/CanalSharp.Logging.NLog/CanalSharp.Logging.NLog.csproj @@ -0,0 +1,17 @@ + + + + + netstandard2.0 + CanalSharp NLog日志支持 + + + + + + + + + + + diff --git a/src/Logging/CanalSharp.Logging.NLog/NLogLoggerFactory.cs b/src/Logging/CanalSharp.Logging.NLog/NLogLoggerFactory.cs new file mode 100644 index 0000000..5908b73 --- /dev/null +++ b/src/Logging/CanalSharp.Logging.NLog/NLogLoggerFactory.cs @@ -0,0 +1,48 @@ +using System; +using CanalSharp.Common.Logging; +using NLog; + +namespace CanalSharp.Logging.NLog +{ + public class NLogLoggerFactory:ICanalSharpLoggerFactory + { + public ICanalSharpLogger CreateLogger(Type type) + { + return new NLogLogger(LogManager.GetLogger("CanalSharp",type)); + } + + internal class NLogLogger:ICanalSharpLogger + { + private readonly Logger _logger; + + public NLogLogger(Logger logger) + { + _logger = logger; + } + public void Debug(string message) + { + _logger.Debug(message); + } + + public void Info(string message) + { + _logger.Info(message); + } + + public void Warning(string message) + { + _logger.Warn(message); + } + + public void Error(string message, Exception exception) + { + _logger.Error(message,exception,null); + } + + public void Trace(string message) + { + _logger.Trace(message); + } + } + } +}