zoukankan      html  css  js  c++  java
  • C# 写日志到文件

    C# 写日志到文件

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;

    namespace psms.util
    {
        class Log
        {
            /// <summary>
            /// 写日志文件
            /// </summary>
            /// <param name="sMsg"></param>
            public static void WriteLog(string sMsg)
            {
                if (sMsg != "")
                {
                    //Random randObj = new Random(DateTime.Now.Millisecond);
                    //int file = randObj.Next() + 1;
                    string filename = DateTime.Now.ToString("yyyyMM") + ".log";
                    try
                    {
                        FileInfo fi = new FileInfo(Application.StartupPath + "\log\" + filename);
                        if (!fi.Exists)
                        {
                            using (StreamWriter sw = fi.CreateText())
                            {
                                sw.WriteLine(DateTime.Now + " " + sMsg + " ");
                                sw.Close();
                            }
                        }
                        else
                        {
                            using (StreamWriter sw = fi.AppendText())
                            {
                                sw.WriteLine(DateTime.Now + " " + sMsg + " ");
                                sw.Close();
                            }
                        }
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }
    }
  • 相关阅读:
    Anaconda设置虚拟环境并打包exe
    [转]Anaconda, conda, pyenv, virtualenv的区别
    [闲记]2020-2-13
    集合笔记
    Python_列表(list)
    LeetCode 1711. 大餐计数 做题小结
    LeetCode 242. 有效的字母异位词 做题小结
    GitHub Actions教程 使用selenium自动化
    LeetCode 5641. 卡车上的最大单元数 做题小结
    git 批量删除文件夹和文件
  • 原文地址:https://www.cnblogs.com/Xujg/p/3823525.html
Copyright © 2011-2022 走看看