HaveFun

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

Event Log 简单研究

这两天要做loging方面的功能,顺便研究了一下EventLog的使用方法。

EventLog 有两个方法 WriteEntry和WriteEvent. 这两个方法的区别,现在看起来只存在于以下方面:

  1. WriteEntry可以写EntryType, 包括Error, Information, Warning ... 而 WriteEvent只能写Information.
  2. WriteEvent可以写string, byte[], Object数据,WriteEntry只能写string(message), byte[]数据。当然自己掉object的tostring写入message就可绕过这一限制。

另外Create log source 或 create log file都需要Administrator权限。原因是查询Security log需要Administrator权限, 而创建log source需要查询所有log file来确保source的名字是全局唯一的。

所以,写log的时候,自需要写source, 不需要写log file name是可以的,它会自己找出这个source属于那个log file.

另外,如果向一个不存在的source写入log, 系统会自己尝试创建一个source, 如果没有指定log file, 系统会默认创建到Application中。而如果当前不是Administrator的化,就会报权限错误,与上面提到的一样。所以不确定source存在并且必须保证写入成功率的话,建议写Application source就可以了。

posted on 2012-09-13 04:32  HaveFun  阅读(334)  评论(0)    收藏  举报