好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

使用C#实现写入系统日志

因为我不想使用自己写文件,我的软件是绿色的,所以把日志写到 windows 日志。

首先告诉大家什么是系统日志,请看下面

如果需要写日志,需要管理员权限,如果没有权限会出现下面异常

?

system.security.securityexception:[未找到源,但未能搜索某些或全部事件日志。 不可访问的日志: security

需要判断当前是否已经存在日志,下面我来创建一个事件叫 [德熙]

?

if (eventlog.sourceexists( "德熙" ))

   {

   eventlog.createeventsource( "德熙" , "application" );

   }

这里的 application 就是写到哪个,一般都是选 application ,可以从图片看到系统的有应用程序、安全、setup、系统几个日志,程序一般都是写到程序

写日志

写日志就不用管理权限

写入可以使用 writeentry ,需要传入写入的日志和内容

?

eventlog.writeentry( "德熙" , "有个不愿告诉你名称的程序在这里写字符串" );

这个方法还有几个重载,可以传入日志类型,是成功、失败还是其他。还可以传入 id ,通过id 可以找到为什么需要写日志,不过需要在自己定义,还可以添加附件,于是我就不需要自己写文件日志。

另外给大家附上一个完整例子

?

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.diagnostics;

namespace consoleapp

{

  /// <summary>

  /// 系统日志

  /// </summary>

  public class packsystemeventlog

  {

   /// <summary>

   /// 错误信息

   /// </summary>

   private static string errorinfo { get ; set ; }

   /// <summary>

   /// 创建系统事件日志分类

   /// </summary>

   /// <param name="eventsourcename">注册事件源(比如说这个日志来源于某一个应用程序)</param>

   /// <param name="logname">日志名称(事件列表显示的名称)</param>

   /// <returns></returns>

   public static bool createsystemeventlogcategory( string eventsourcename, string logname)

   {

    bool createresult = false ;

    try

    {

     if (!eventlog.sourceexists(eventsourcename))

     {

      eventlog.createeventsource(eventsourcename, logname);

     }

     createresult = true ;

    }

    catch (exception ex)

    {

     createresult = false ;

     errorinfo = ex.message;

    }

    return createresult;

   }

   /// <summary>

   /// 删除系统事件日志分类

   /// </summary>

   /// <param name="eventsource">eventname事件源</param>

   /// <returns></returns>

   public static bool removesystemeventsourcecategory( string eventsource)

   {

    bool createresult = false ;

    try

    {

     if (eventlog.sourceexists(eventsource))

     {

      eventlog.deleteeventsource(eventsource, "." );

     }

     createresult = true ;

    }

    catch (exception ex)

    {

     createresult = false ;

     errorinfo = ex.message;

    }

    return createresult;

   }

   /// <summary>

   /// 向系统日志中写入日志

   /// </summary>

   /// <param name="eventsource">事件源</param>

   /// <param name="msg">写入日志信息</param>

   /// <param name="type">日志文本分类(警告、信息、错误)</param>

   /// <returns></returns>

   public static bool writesystemeventlog( string eventsource, string msg, eventlogentrytype type)

   {

    bool writeresult = false ;

    try

    {

     if (!eventlog.sourceexists(eventsource))

     {

      writeresult = false ;

      errorinfo = "日志分类不存在!" ;    

     }

     else

     {

      eventlog.writeentry(eventsource, msg, type);

      writeresult = true ;

     }

    }

    catch (exception ex)

    {

     writeresult = false ;

     errorinfo = ex.message;

    }

    return writeresult;

   }

   /// <summary>

   /// 删除事件源中logname(好像删除了所有的该分类的日志)

   /// </summary>

   /// <param name="eventsource"></param>

   /// <param name="logname"></param>

   /// <returns></returns>

   public static bool removesystemeventlog( string eventsource, string logname)

   {

    bool removeresult = false ;

    try

    {

     if (!eventlog.sourceexists(eventsource))

     {

      removeresult = false ;

      errorinfo = "日志分类不存在!" ;

     }

     else

     {

      eventlog.delete(logname);

      removeresult = true ;

     }

    }

    catch (exception ex)

    {

     removeresult = false ;

     errorinfo = ex.message;

    }

    return removeresult;

   }

   /// <summary>

   /// 获取错误信息

   /// </summary>

   /// <returns></returns>

   public static string geterrormessage()

   {

    return errorinfo;

   }

  }

}

原文链接:https://lindexi.github.io/lindexi/post/C-写系统日志.html

dy("nrwz");

查看更多关于使用C#实现写入系统日志的详细内容...

  阅读:39次