|
| 1 | +using System; |
| 2 | +using FubarDev.FtpServer; |
| 3 | +using NLog; |
| 4 | + |
| 5 | +namespace Wikiled.YiScanner.Server |
| 6 | +{ |
| 7 | + public class FtpLogForNLog : IFtpLog |
| 8 | + { |
| 9 | + private readonly ILogger logger; |
| 10 | + |
| 11 | + private readonly string remoteAddress; |
| 12 | + |
| 13 | + private readonly string remoteIp; |
| 14 | + |
| 15 | + private readonly int? remotePort; |
| 16 | + |
| 17 | + public FtpLogForNLog(FtpConnection connection) |
| 18 | + { |
| 19 | + logger = LogManager.GetLogger("FubarDev.FtpServer.FtpConnection"); |
| 20 | + remoteAddress = connection.RemoteAddress.ToString(true); |
| 21 | + remoteIp = connection.RemoteAddress.IpAddress; |
| 22 | + remotePort = connection.RemoteAddress.IpPort; |
| 23 | + } |
| 24 | + |
| 25 | + public FtpLogForNLog(Type type) |
| 26 | + { |
| 27 | + logger = LogManager.GetLogger(type.FullName); |
| 28 | + } |
| 29 | + |
| 30 | + public FtpLogForNLog(string name) |
| 31 | + { |
| 32 | + logger = LogManager.GetLogger(name); |
| 33 | + } |
| 34 | + |
| 35 | + public void Trace(string format, params object[] args) |
| 36 | + { |
| 37 | + Log(LogLevel.Trace, null, format, args); |
| 38 | + } |
| 39 | + |
| 40 | + public void Trace(Exception ex, string format, params object[] args) |
| 41 | + { |
| 42 | + Log(LogLevel.Trace, ex, format, args); |
| 43 | + } |
| 44 | + |
| 45 | + public void Debug(string format, params object[] args) |
| 46 | + { |
| 47 | + Log(LogLevel.Debug, null, format, args); |
| 48 | + } |
| 49 | + |
| 50 | + public void Debug(Exception ex, string format, params object[] args) |
| 51 | + { |
| 52 | + Log(LogLevel.Debug, ex, format, args); |
| 53 | + } |
| 54 | + |
| 55 | + public void Info(string format, params object[] args) |
| 56 | + { |
| 57 | + Log(LogLevel.Info, null, format, args); |
| 58 | + } |
| 59 | + |
| 60 | + public void Info(Exception ex, string format, params object[] args) |
| 61 | + { |
| 62 | + Log(LogLevel.Info, ex, format, args); |
| 63 | + } |
| 64 | + |
| 65 | + public void Warn(string format, params object[] args) |
| 66 | + { |
| 67 | + Log(LogLevel.Warn, null, format, args); |
| 68 | + } |
| 69 | + |
| 70 | + public void Warn(Exception ex, string format, params object[] args) |
| 71 | + { |
| 72 | + Log(LogLevel.Warn, ex, format, args); |
| 73 | + } |
| 74 | + |
| 75 | + public void Error(string format, params object[] args) |
| 76 | + { |
| 77 | + Log(LogLevel.Error, null, format, args); |
| 78 | + } |
| 79 | + |
| 80 | + public void Error(Exception ex, string format, params object[] args) |
| 81 | + { |
| 82 | + Log(LogLevel.Error, ex, format, args); |
| 83 | + } |
| 84 | + |
| 85 | + public void Fatal(string format, params object[] args) |
| 86 | + { |
| 87 | + Log(LogLevel.Fatal, null, format, args); |
| 88 | + } |
| 89 | + |
| 90 | + public void Fatal(Exception ex, string format, params object[] args) |
| 91 | + { |
| 92 | + Log(LogLevel.Fatal, ex, format, args); |
| 93 | + } |
| 94 | + |
| 95 | + private void Log(LogLevel logLevel, Exception ex, string format, params object[] args) |
| 96 | + { |
| 97 | + var message = args.Length == 0 ? format : string.Format(format, args); |
| 98 | + logger.Log(new LogEventInfo(logLevel, logger.Name, message) |
| 99 | + { |
| 100 | + Properties = |
| 101 | + { |
| 102 | + ["RemoteAddress"] = remoteAddress, |
| 103 | + ["RemoteIp"] = remoteIp, |
| 104 | + ["RemotePort"] = remotePort, |
| 105 | + }, |
| 106 | + Exception = ex, |
| 107 | + }); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments