103 lines
3.5 KiB
C#
103 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace XYLEM.Base
|
|
{
|
|
public static class LogMessageTool
|
|
{
|
|
/// <summary>
|
|
/// Return Type Name as a string
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
static public string TypeName<T>(this T obj)
|
|
{
|
|
Type t;
|
|
if (obj == null)
|
|
{
|
|
t = typeof(T);
|
|
}
|
|
else
|
|
{
|
|
t = obj.GetType();
|
|
}
|
|
return t.Name;
|
|
}
|
|
|
|
public static string ToMessageText(this Exception ex)
|
|
{
|
|
if (ex != null)
|
|
{
|
|
var message = "";
|
|
var innerMessage = "";
|
|
if (ex.InnerException != null)
|
|
{
|
|
innerMessage = ToMessageText(ex.InnerException);
|
|
}
|
|
if ((ex.Message != null) && !String.IsNullOrEmpty(ex.Message))
|
|
{
|
|
message = ex.Message;
|
|
}
|
|
if (!String.IsNullOrEmpty(innerMessage))
|
|
{
|
|
message += (!String.IsNullOrEmpty(message) ? Environment.NewLine : "") + innerMessage;
|
|
}
|
|
return message;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
public static string ToFuncText(bool isError, string className, string functionName)
|
|
{
|
|
var message = (isError ? "Error in class" : "Class ") + className + " and func. " + functionName;
|
|
#if(DEBUG)
|
|
System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff ") + message);
|
|
#endif
|
|
return message;
|
|
}
|
|
|
|
public static string ToInitText(bool isError, string className)
|
|
{
|
|
var message = (isError ? "Error in init of " : "Init of ") + className;
|
|
#if(DEBUG)
|
|
System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff ") + message);
|
|
#endif
|
|
return message;
|
|
}
|
|
|
|
public static string ToInitText(bool isError, string className, string dataInfo)
|
|
{
|
|
return ToInitText(isError, className + " info = " + dataInfo);
|
|
}
|
|
|
|
public static string ToFuncErrorText(this Exception ex, string className, string functionName)
|
|
{
|
|
return ToFuncText(true, className, functionName + ((ex == null) ? "" : Environment.NewLine + "Message = " + ex.ToMessageText()));
|
|
}
|
|
|
|
public static string ToFuncErrorText(this Exception ex, string className, string functionName, string dataInfo)
|
|
{
|
|
return ToFuncText(true, className, functionName + Environment.NewLine + dataInfo + ((ex == null) ? "" : Environment.NewLine + "Message = " + ex.ToMessageText()));
|
|
}
|
|
|
|
public static string ToFuncErrorText(string className, string functionName, string dataInfo)
|
|
{
|
|
return ToFuncText(true, className, functionName + Environment.NewLine + dataInfo);
|
|
}
|
|
|
|
public static string ToFuncErrorText(string className, string functionName)
|
|
{
|
|
return ToFuncText(true, className, functionName);
|
|
}
|
|
|
|
public static string ToInitErrorText(this Exception ex, string className)
|
|
{
|
|
return ToInitText(true, className + ((ex == null) ? "" : Environment.NewLine + "Message = " + ex.ToMessageText()));
|
|
}
|
|
}
|
|
}
|