[原创]可显示行号,类名,方法名的c#日志工具类


/****************************************************************************
*
*Created by luozheng on 2018  
*CLR版本: 4.0.30319.42000
*机器名称:ZYWA-X
*公司名称 xxx科技有限公司
*命名空间:cn.qssq666.cn
*文件名: JSONCreateUtil
*唯一标识:6bb27665-86a7-4d21-a6ed-f8fb18436e63
*当前的用户域:ZYWA-X
*电子邮箱:[email protected]
*创建时间:2018/9/28 14:25:20
*描述:
*============================================================================
*修改标记
*修改时间:2018/9/28 14:25:20
*修改人: XXXX
*版本号: V1.0.0.0
*描述:
*****************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LOZN
{
    public class LogUtil
    {
        private static string TAG = "[Log]";

        public static void writeLog(string msg)
        {


            Console.WriteLine(TAG + getStackFrameLocationInfo() + msg);
        }

        public static void writeLog(string msg, params object[] values)
        {
            //Console.WriteLine(" File: {0}", sf.GetFileName());                                                //文件名
            //Console.WriteLine(" Method: {0}", sf.GetMethod().Name);                                 //函数名
            //Console.WriteLine(" Line Number: {0}", sf.GetFileLineNumber());                  //文件行号
            //Console.WriteLine(" Column Number: {0}", sf.GetFileColumnNumber());

            Console.WriteLine(TAG + getStackFrameLocationInfo() + msg, values);
        }

        public static String getStackFrameLocationInfo()
        {
            System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
            StackFrame sf = st.GetFrame(3);
            if (sf == null)
            {
                return "[unknownClass]";
            }
            //StackTrace st = new StackTrace();
            //StackFrame sf = st.GetFrame(5);
            /*
             * 0-12 17:37:09.177 21187-21341/? I/MSF.S.AppProcessManager: SendToApp PUSH process:com.tencent.mobileqq fromServiceMsg: FromServiceMsg msName:onRecvPushMsg ssoSeq:-381490022 qssq666serviceCmd:OnlinePush.ReqPush appSeq:-381490022 failCode:1000 cost=8 needBoot=true
10-12 17:37:09.188 21187-21341/? I/MSF.S.AppProcessManager: Se
*/


            //   string filename = System.IO.Path.GetFileName(/*fullPath*/);//文件名  “bwn.”
            //string extension = System.IO.Path.GetExtension(fullPath);//扩展名 “.aspx”
            string className = System.IO.Path.GetFileNameWithoutExtension(sf.GetFileName());
            return String.Format(@"[{0}:{1}->{2}{3}:{4}]", DateTime.Now.ToLongTimeString(),
                className, 
                sf.GetMethod(), 

                     sf.GetFileLineNumber(),
                sf.GetFileColumnNumber());
        }

        public static void writeTagLog(string tag, string msg, params object[] values)
        {
            if (values != null && values.Length > 0)
            {
                Console.WriteLine(TAG + " [" + tag + "]" + msg + getStackFrameLocationInfo(), values);

            }
            else
            {

                Console.WriteLine(TAG + "[" + tag + "]" + getStackFrameLocationInfo() + msg);

            }
        }

        public static void writeStackLog(string v)
        {
            StackTrace st = new StackTrace(true);
            string str = st.ToString();
            Console.WriteLine(TAG + getStackFrameLocationInfo() + "-" + str);
        }
    }
}

效果


image.png

你可能感兴趣的:([原创]可显示行号,类名,方法名的c#日志工具类)