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}】服务启动!!\r\n", DateTime.Now));

 

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

 

转载于:https://www.cnblogs.com/qiujianfeng/p/10106800.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值