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);
                    }
                }
            }
        }
    }
  • 相关阅读:
    完全数
    100c之29:求具有abcd= ( ab + cd )^2 性质的四位数
    完全数
    100c之22:计算车速
    100c之21:4位反序数
    100c之28:回文数
    AJAX
    网站运营十条定律 刚刚好
    QQ也流氓了 刚刚好
    服务器变肉鸡了 刚刚好
  • 原文地址:https://www.cnblogs.com/Xujg/p/3823525.html
Copyright © 2011-2022 走看看