Skip to content

Commit

Permalink
Merge pull request #2783 from celeron533/nlog
Browse files Browse the repository at this point in the history
Using NLog as the logger

No wrapper is used since we would use NLog for some time.
Currently the UI is not fully ready to select all log levels.
  • Loading branch information
celeron533 authored Jan 30, 2020
2 parents a67198f + 6922ba1 commit 94f00f6
Show file tree
Hide file tree
Showing 41 changed files with 892 additions and 650 deletions.
9 changes: 6 additions & 3 deletions shadowsocks-csharp/Controller/FileManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using NLog;
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
Expand All @@ -7,6 +8,8 @@ namespace Shadowsocks.Controller
{
public static class FileManager
{
private static Logger logger = LogManager.GetCurrentClassLogger();

public static bool ByteArrayToFile(string fileName, byte[] content)
{
try
Expand All @@ -17,7 +20,7 @@ public static bool ByteArrayToFile(string fileName, byte[] content)
}
catch (Exception ex)
{
Logging.Error(ex);
logger.Error(ex);
}
return false;
}
Expand Down Expand Up @@ -57,7 +60,7 @@ public static string NonExclusiveReadAllText(string path, Encoding encoding)
}
catch (Exception ex)
{
Logging.Error(ex);
logger.Error(ex);
throw ex;
}
}
Expand Down
5 changes: 3 additions & 2 deletions shadowsocks-csharp/Controller/HotkeyReg.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;

using NLog;
using Shadowsocks.Controller.Hotkeys;
using Shadowsocks.Model;

namespace Shadowsocks.Controller
{
static class HotkeyReg
{
private static Logger logger = LogManager.GetCurrentClassLogger();
public static void RegAllHotkeys()
{
var hotkeyConfig = Configuration.Load().hotkey;
Expand Down Expand Up @@ -60,7 +61,7 @@ public static bool RegHotkeyFromString(string hotkeyStr, string callbackName, Ac
var hotkey = HotKeys.Str2HotKey(hotkeyStr);
if (hotkey == null)
{
Logging.Error($"Cannot parse hotkey: {hotkeyStr}");
logger.Error($"Cannot parse hotkey: {hotkeyStr}");
onComplete?.Invoke(RegResult.ParseError);
return false;
}
Expand Down
12 changes: 7 additions & 5 deletions shadowsocks-csharp/Controller/I18N.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualBasic.FileIO;
using NLog;
using Shadowsocks.Properties;
using Shadowsocks.Util;
using System.Collections.Generic;
Expand All @@ -9,9 +10,10 @@

namespace Shadowsocks.Controller
{

public static class I18N
{
private static Logger logger = LogManager.GetCurrentClassLogger();

public const string I18N_FILE = "i18n.csv";

private static Dictionary<string, string> _strings = new Dictionary<string, string>();
Expand Down Expand Up @@ -47,12 +49,12 @@ private static void Init(string res, string locale)
}
if (targetIndex != -1 && enIndex != targetIndex)
{
Logging.Info($"Using {localeNames[targetIndex]} translation for {locale}");
logger.Info($"Using {localeNames[targetIndex]} translation for {locale}");
}
else
{
// Still not found, exit
Logging.Info($"Translation for {locale} not found");
logger.Info($"Translation for {locale} not found");
return;
}
}
Expand Down Expand Up @@ -85,10 +87,10 @@ static I18N()
}
else
{
Logging.Info("Using external translation");
logger.Info("Using external translation");
i18n = File.ReadAllText(I18N_FILE, Encoding.UTF8);
}
Logging.Info("Current language is: " + locale);
logger.Info("Current language is: " + locale);
Init(i18n, locale);
}

Expand Down
119 changes: 119 additions & 0 deletions shadowsocks-csharp/Controller/LoggerExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System;
using System.ComponentModel;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Diagnostics;
using System.Text;
using Shadowsocks.Util.SystemProxy;

namespace NLog
{
public static class LoggerExtension
{
public static void Dump(this Logger logger, string tag, byte[] arr, int length)
{
if (logger.IsTraceEnabled)
{
var sb = new StringBuilder($"{Environment.NewLine}{tag}: ");
for (int i = 0; i < length - 1; i++)
{
sb.Append($"0x{arr[i]:X2}, ");
}
sb.Append($"0x{arr[length - 1]:X2}");
sb.Append(Environment.NewLine);
logger.Trace(sb.ToString());
}
}

public static void Debug(this Logger logger, EndPoint local, EndPoint remote, int len, string header = null, string tailer = null)
{
if (logger.IsDebugEnabled)
{
if (header == null && tailer == null)
logger.Debug($"{local} => {remote} (size={len})");
else if (header == null && tailer != null)
logger.Debug($"{local} => {remote} (size={len}), {tailer}");
else if (header != null && tailer == null)
logger.Debug($"{header}: {local} => {remote} (size={len})");
else
logger.Debug($"{header}: {local} => {remote} (size={len}), {tailer}");
}
}

public static void Debug(this Logger logger, Socket sock, int len, string header = null, string tailer = null)
{
if (logger.IsDebugEnabled)
{
logger.Debug(sock.LocalEndPoint, sock.RemoteEndPoint, len, header, tailer);
}
}

public static void LogUsefulException(this Logger logger, Exception e)
{
// just log useful exceptions, not all of them
if (e is SocketException)
{
SocketException se = (SocketException)e;
if (se.SocketErrorCode == SocketError.ConnectionAborted)
{
// closed by browser when sending
// normally happens when download is canceled or a tab is closed before page is loaded
}
else if (se.SocketErrorCode == SocketError.ConnectionReset)
{
// received rst
}
else if (se.SocketErrorCode == SocketError.NotConnected)
{
// The application tried to send or receive data, and the System.Net.Sockets.Socket is not connected.
}
else if (se.SocketErrorCode == SocketError.HostUnreachable)
{
// There is no network route to the specified host.
}
else if (se.SocketErrorCode == SocketError.TimedOut)
{
// The connection attempt timed out, or the connected host has failed to respond.
}
else
{
logger.Warn(e);
}
}
else if (e is ObjectDisposedException)
{
}
else if (e is Win32Exception)
{
var ex = (Win32Exception)e;

// Win32Exception (0x80004005): A 32 bit processes cannot access modules of a 64 bit process.
if ((uint)ex.ErrorCode != 0x80004005)
{
logger.Warn(e);
}
}
else if (e is ProxyException)
{
var ex = (ProxyException)e;
switch (ex.Type)
{
case ProxyExceptionType.FailToRun:
case ProxyExceptionType.QueryReturnMalformed:
case ProxyExceptionType.SysproxyExitError:
logger.Error($"sysproxy - {ex.Type.ToString()}:{ex.Message}");
break;
case ProxyExceptionType.QueryReturnEmpty:
case ProxyExceptionType.Unspecific:
logger.Error($"sysproxy - {ex.Type.ToString()}");
break;
}
}
else
{
logger.Warn(e);
}
}
}
}
Loading

0 comments on commit 94f00f6

Please sign in to comment.