zoukankan      html  css  js  c++  java
  • [C#]写系统日志和写日志文件

    事件日志:
          使用EventLog   类,如:
        下面的示例创建源   MySource(如果尚未存在),并在事件日志   MyNewLog   中写入一项。

    using   System;
    using   System.Diagnostics;
    using   System.Threading;
                               
    class   MySample{

            public   static   void   Main(){
           
                    //   Create   the   source,   if   it   does   not   already   exist.
                    if(!EventLog.SourceExists( "MySource ")){
                            EventLog.CreateEventSource( "MySource ",   "MyNewLog ");
                            Console.WriteLine( "CreatingEventSource ");
                    }
                                   
                    //   Create   an   EventLog   instance   and   assign   its   source.
                    EventLog   myLog   =   new   EventLog();
                    myLog.Source   =   "MySource ";
                   
                    //   Write   an   informational   entry   to   the   event   log.        
                    myLog.WriteEntry( "Writing   to   event   log. ");
                   
            }
    }
         

    文本文件:
    如下代码向一个文本文件写入字符内容
    using   System;
    using   System.IO;

    class   Test  
    {
            public   static   void   Main()  
            {
                    //   Create   an   instance   of   StreamWriter   to   write   text   to   a   file.
                    //   The   using   statement   also   closes   the   StreamWriter.
                    using   (StreamWriter   sw   =   new   StreamWriter( "TestFile.txt "))  
                    {
                            //   Add   some   text   to   the   file.
                            sw.Write( "This   is   the   ");
                            sw.WriteLine( "header   for   the   file. ");
                            sw.WriteLine( "------------------- ");
                            //   Arbitrary   objects   can   also   be   written   to   the   file.
                            sw.Write( "The   date   is:   ");
                            sw.WriteLine(DateTime.Now);
                    }
            }
    }

     ///   <summary>  
      ///   生成系统日志。  
      ///   data: 2005-10-07  
      ///   description: 由于在写入数据的时候报IO错误,因此暂时停止日志写入  
      ///   author: chenlihua  
      ///   </summary>  
      ///   <param   name="Description">所记录日志描述。</param>  
      ///   <param   name="Content">所记录日志内容。</param>  
      public   static   void   CreateSqlLog(string   Description,string   Content)  
      {  
      /*  
      string   strTmp   ="";  
      string   FilePath   =   @"e:\syslog\sells_log\att_log\";  
       
      strTmp   =   DateTime.Now.Year.ToString()+"\\"   +DateTime.Now.Month.ToString()+"\\"+DateTime.Now.Day.ToString()+"\\";  
      FilePath   +=   strTmp;  
       
      if   (!Directory.Exists(FilePath))  
      {  
      Directory.CreateDirectory(FilePath);  
      }//   文件夹操作完成。  
       
      FilePath   +=   "Att_log_";  
       
      FilePath   +=   DateTime.Now.Year.ToString()+"_"+DateTime.Now.Month.ToString()+"_"+DateTime.Now.Day.ToString()   +   "_"+DateTime.Now.Hour.ToString()+".txt";  
       
      StreamWriter   sw;  
      if   (!File.Exists(FilePath))  
      {  
      sw   =   File.CreateText(FilePath);  
      }  
      else  
      {  
      sw   =   File.AppendText(FilePath);  
      }  
       
      sw.WriteLine("[system   time]:   "+DateTime.Now.ToString());  
      sw.WriteLine("---------------------------------------------------------------------------------------");  
       
      sw.WriteLine(Description.ToString()   +   ":"   +   Content.ToString());  
      sw.WriteLine();  
       
      sw.Close();  
      */  
      }  

  • 相关阅读:
    详解应对平台高并发的分布式调度框架TBSchedule
    利用httpd配置虚拟目录创建下载站点
    在centos7下用http搭建配置svn服务
    centos7/rhel7下配置PXE+Kickstart自动安装linux系统
    centos7破解安装confluence5.9.11
    centos7破解安装jira6.3.6(含Agile)
    mysql5.6做单向主从复制Replication
    解决忘记mysql中的root用户密码问题
    centos7安装mysql5.6(rpm包安装)
    centos7/RHEL7最小化系统安装gnome图形界面
  • 原文地址:https://www.cnblogs.com/boneking/p/1338515.html
Copyright © 2011-2022 走看看