zoukankan      html  css  js  c++  java
  • [ 转载]C#好用的自定义日志功能实现

    using System;
     using System.Collections.Generic;
     using System.ComponentModel;
     using System.Data;
     using System.Drawing;
     using System.Text;
     using System.Windows.Forms;
     using System.IO;
     
     namespace LogDemo
     {
         public partial class Form1 : Form
         {
             public Form1()
             {
                 InitializeComponent();
             }
             /// <summary>
             /// 操作文件实现日志
             /// </summary>
             /// <param name="msg">日志内容</param>
             /// <param name="lonPath">存放日志的目录,如果想默认存在项目的Debag下,请输入Null</param>
             private void logger(String msg,string lonPath)
             {
                 string LogPath = null;
                 if (lonPath != null)
                 {
                     LogPath = lonPath;
                 }
                 else
                 {
                     LogPath =Application.StartupPath;
                 }
                 string filePath = LogPath + "\\" + DateTime.Now.Year.ToString()+"" + DateTime.Now.Month.ToString()+"";
                 string fileName = DateTime.Now.Day.ToString() + "" + ".Log";
                 string filePathAll = filePath + "\\" + fileName;
                 if (!System.IO.Directory.Exists(filePath))
                 {
                     System.IO.Directory.CreateDirectory(filePath);
                 }
                 FileStream filestream = new FileStream(filePathAll, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
                 StreamWriter writer = new StreamWriter(filestream, System.Text.Encoding.Default);
                 writer.BaseStream.Seek(0, SeekOrigin.End);
                 writer.WriteLine("{0} {1}", DateTime.Now.TimeOfDay , msg);
                 writer.Flush();
                 writer.Close();
                 filestream.Close();
             }
     
             //测试方法
             private void button1_Click(object sender, EventArgs e)
             {
                 byte str=0 ;
     
                 try
                 {
                     if (true)
                     {
                         str = (byte)Convert.ToSByte(600);
                     }
                     else
                     {
                         str = (byte)Convert.ToSByte(500);
                     }
                 }
                 catch(Exception ex)
                 {
                     logger(ex.Message,null);
                 }
             }
         }
     }
  • 相关阅读:
    bzoj2733 永无乡 平衡树按秩合并
    bzoj2752 高速公路 线段树
    bzoj1052 覆盖问题 二分答案 dfs
    bzoj1584 打扫卫生 dp
    bzoj1854 游戏 二分图
    bzoj3316 JC loves Mkk 二分答案 单调队列
    bzoj3643 Phi的反函数 数学 搜索
    有一种恐怖,叫大爆搜
    BZOJ3566 概率充电器 概率dp
    一些奇奇怪怪的过题思路
  • 原文地址:https://www.cnblogs.com/jizonghai/p/3070691.html
Copyright © 2011-2022 走看看