diff --git a/Emby.Common.Implementations/Emby.Common.Implementations.csproj b/Emby.Common.Implementations/Emby.Common.Implementations.csproj index 00c90d16e..5b1defccb 100644 --- a/Emby.Common.Implementations/Emby.Common.Implementations.csproj +++ b/Emby.Common.Implementations/Emby.Common.Implementations.csproj @@ -228,6 +228,7 @@ + diff --git a/Emby.Common.Implementations/IO/SharpCifs/Util/DbsHelper/Log.cs b/Emby.Common.Implementations/IO/SharpCifs/Util/DbsHelper/Log.cs new file mode 100644 index 000000000..eebf7c6b2 --- /dev/null +++ b/Emby.Common.Implementations/IO/SharpCifs/Util/DbsHelper/Log.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + + +namespace SharpCifs.Util.DbsHelper +{ + public class Log + { + /// + /// コンソールへのログ出力を行うか否か + /// + public static bool IsActive { get; set; } = false; + + public static void Out(string message) + { + if (!Log.IsActive + || string.IsNullOrEmpty(message)) + return; + + var msg = DateTime.Now.ToString("HH:mm:ss.fff") + + ": [ThID: " + + System.Environment.CurrentManagedThreadId.ToString().PadLeft(3) + + " " + + message; + + Debug.WriteLine(msg); + Console.WriteLine(msg); + } + + /// + /// 例外のログ出力を行う。 + /// + /// + public static void Out(Exception ex) + { + if (!Log.IsActive + || ex == null) + return; + + Log.Out($"{ex}"); + var message = Log.GetHighlighted(Log.GetErrorString(ex)); + Log.Out(message); + } + + /// + /// Cast string-arrary to Highlighted message + /// 文字列配列を強調メッセージ形式文字列に変換する。 + /// + /// + /// + private static string GetHighlighted(params System.String[] messages) + { + var time = DateTime.Now; + var list = new List(); + + list.Add(""); + list.Add(""); + list.Add(time.ToString("HH:mm:ss.fff") + ":"); + list.Add("##################################################"); + list.Add("#"); + + foreach (string message in messages) + { + var lines = message.Replace("\r\n", "\n").Replace("\r", "\n").Trim('\n').Split('\n'); + foreach (var line in lines) + { + list.Add($"# {line}"); + } + } + + list.Add("#"); + list.Add("##################################################"); + list.Add(""); + list.Add(""); + + return string.Join("\r\n", list); + } + + /// + /// Get Formatted Exception-Info string-array + /// 例外情報を整形した文字列配列を返す。 + /// + /// + /// + private static string[] GetErrorString(Exception ex) + { + var list = new List(); + + if (ex.Message != null) + { + list.Add(ex.Message ?? ""); + list.Add(""); + } + + if (ex.StackTrace != null) + { + list.AddRange(ex.StackTrace.Split(new string[] { "場所", "at " }, + StringSplitOptions.None) + .AsEnumerable() + .Select(row => "\r\nat " + row)); + } + + if (ex.InnerException != null) + { + //InnerExceptionを再帰取得する。 + list.Add(""); + list.Add("Inner Exception"); + list.AddRange(Log.GetErrorString(ex.InnerException)); + } + + return list.ToArray(); + } + } +}