C# 创建Log日志

日志所要保存的路径 

public static string LogLocation = "D:\\MyLog";

参数说明: 

ID:日志所在文件夹名称

Msg:日志的具体信息

public static bool LogMsg(string ID, string Msg)
{
    return LogMsg(ID, DateTime.Now, Msg);
}

 参数说明: 

ID:日志所在文件夹名称

LogDate:日志创建的日期时间

Msg:日志的具体信息

public static bool LogMsg(string ID, DateTime LogDate, string Msg)
{
    try
    {
        if (ID == null || ID.Trim() == "")
            return false;
        if (LogLocation == "")
            return false;

        string path = LogLocation + "\\" + ID + "\\" + LogDate.ToString("yyyyMM");
        string file = path + "\\" + LogDate.ToString("yyyyMMdd") + ".txt";
        if (!System.IO.Directory.Exists(path))
            System.IO.Directory.CreateDirectory(path);
        if (!System.IO.File.Exists(file))
            System.IO.File.Create(file).Close();

        System.IO.StreamWriter sw = new System.IO.StreamWriter(file, true);
        sw.WriteLine(FormatStr(Msg));
        sw.Close();
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

示例:

LogMsg("【系统对接】\\【人事接口对接】", "接口调前;传的值:postString='" + postString + "'");

LogMsg("【系统对接】\\【人事接口对接】", "接口调用返回值;人员编号:ID='" + model.ID + "';结果:" + resultStr + "");

LogMsg("【系统对接】\\【人事接口对接】", "接口调用返回值;人员编号:ID='" + model.ID + "';结果:" + resultStr + "");

猜你喜欢

转载自blog.csdn.net/weixin_41392824/article/details/82256002
今日推荐