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);
                    }
                }
            }
        }
    }
  • 相关阅读:
    前世今生:Hive、Shark、spark SQL
    spark streaming 6: BlockGenerator、RateLimiter
    spark streaming 5: InputDStream
    spark streaming 4: DStreamGraph JobScheduler
    常见css水平自适应布局
    js动态加载以及确定加载完成的代码
    如何判断css是否加载完成
    翻书特效
    jquery 事件冒泡的介绍以及如何阻止事件冒泡
    phonegap之android原生日历调用
  • 原文地址:https://www.cnblogs.com/Xujg/p/3823525.html
Copyright © 2011-2022 走看看