• C# .NET写入记事本(规范版)


    指定Log文件夹下面,按照月份归类,每天一个日志,如下图:

    代码如下:

            public static string getServerPath(string ServiceName)
            {
                string currentPath = AppDomain.CurrentDomain.BaseDirectory;
                currentPath = currentPath.Substring(0, currentPath.LastIndexOf("\")); //取出最后一个
                currentPath = currentPath.Substring(0, currentPath.LastIndexOf("\")); //去除最后一个
                currentPath = currentPath.Substring(0, currentPath.LastIndexOf("\")); //去除最后一个
                currentPath += "\Log";
                if (Directory.Exists(currentPath))
                {
                    string strDate = DateTime.Now.ToString("yyyyMM");
                    currentPath += "\" + ServiceName;
                    if (Directory.Exists(currentPath))
                    {
                        currentPath += "\" + strDate;
                        if (Directory.Exists(currentPath))
                        {
                            currentPath += "\" + DateTime.Now.ToString("yyyyMMdd") + ".log";
                            if (File.Exists(currentPath))
                            {
                                return currentPath;
                            }
                            else
                            {
                                FileStream fs = File.Create(currentPath);
                                fs.Flush();
                                fs.Close();
                                return currentPath;
                            }
                        }
                        else
                        {
                            Directory.CreateDirectory(currentPath);
                            currentPath += "\" + DateTime.Now.ToString("yyyyMMdd") + ".log";
    
                            FileStream fs = File.Create(currentPath);
                            fs.Flush();
                            fs.Close();
                            return currentPath;
                        }
                    }
                    else
                    {
                        Directory.CreateDirectory(currentPath);
                        currentPath += "\" + strDate;
                        Directory.CreateDirectory(currentPath);
                        currentPath += "\" + DateTime.Now.ToString("yyyyMMdd") + ".log";
    
                        FileStream fs = File.Create(currentPath);
                        fs.Flush();
                        fs.Close();
                        return currentPath;
                    }
                }
                else
                {
                    return currentPath + "\WxServerLog.log";
                }
            }

    使用:

    string logPath_Report = getServerPath("WxService");
    File.AppendAllText(logPath_Report, string.Format("【{0}】服务启动!!
    ", DateTime.Now));

     getServerPath("WxService")                                               //调用方法,WxService是文件夹名称

  • 相关阅读:
    使用360安全卫士在线对本机电脑进行重装系统
    分享个人电脑上的文件
    Docker初探之运行RabbitMQ消息队列服务
    Docker初探之运行MySQL
    Docker初探之常用命令实践
    Docker初探之Windows篇
    SignalR入门之多平台SignalR服务端
    SignalR入门之从外部访问持久性连接或Hub
    SignalR入门之Hub
    SignalR入门之小试身手
  • 原文地址:https://www.cnblogs.com/qiujianfeng/p/10106800.html
Copyright © 2020-2023  润新知