dotNET下使用EventLog類寫日志,出現權限不夠的解決方法!(即寫Windows日志權限不夠)
Posted on 2007-04-13 15:51 talantlee 阅读(1233) 评论(0) 收藏 举报
解決方法一
提升ASPNET帐户的权限可以直接在Windows系统管理中给ASPNET帐户添加对系统的读写权限,但是这样做存在很严重的安全问题,asp.net进程有直接读写操作系统的权限,将给系统带来很大的安全隐患.
建議解決方法:
1>打開Regedit ,并找到下面的節點HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog
2>增加兩級項 比如:MyLog --WebLog
3>新寫一個安裝類
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Configuration.Install;
namespace EventLogSourceInstaller


{
[RunInstaller(true)]
public class MyEventLogInstaller: Installer

{
public EventLogInstaller myEventLogInstaller;
public MyEventLogInstaller()

{
//Create Instance of EventLogInstaller
myEventLogInstaller = new EventLogInstaller();
// Set the Source of Event Log, to be created.
myEventLogInstaller.Source = "WebLog";
// Set the Log that source is created in
myEventLogInstaller.Log = "TDDN";
Installers.Add(myEventLogInstaller);
}
}
}
將上面的編譯成dll,在.NET下的cmd注冊:InstallUtil EventLogSourceInstaller.dll
5>使用EventLog.WriteLog
EventLog eventLog = null;

if (!(EventLog.SourceExists("WebLog"))){

EventLog.CreateEventSource("WebLog", "MyLog");

}
if (eventLog == null){

eventLog = new EventLog("MyLog");
eventLog.Source = "WebLog";

}

eventLog.WriteEntry("This is error!", EventLogEntryType.Error);
提升ASPNET帐户的权限可以直接在Windows系统管理中给ASPNET帐户添加对系统的读写权限,但是这样做存在很严重的安全问题,asp.net进程有直接读写操作系统的权限,将给系统带来很大的安全隐患.
建議解決方法:
1>打開Regedit ,并找到下面的節點HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog
2>增加兩級項 比如:MyLog --WebLog
3>新寫一個安裝類
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Configuration.Install;
namespace EventLogSourceInstaller 

{
[RunInstaller(true)]
public class MyEventLogInstaller: Installer
{
public EventLogInstaller myEventLogInstaller;
public MyEventLogInstaller()
{
//Create Instance of EventLogInstaller
myEventLogInstaller = new EventLogInstaller();
// Set the Source of Event Log, to be created.
myEventLogInstaller.Source = "WebLog";
// Set the Log that source is created in
myEventLogInstaller.Log = "TDDN";
Installers.Add(myEventLogInstaller);
}
}
}將上面的編譯成dll,在.NET下的cmd注冊:InstallUtil EventLogSourceInstaller.dll
5>使用EventLog.WriteLog
EventLog eventLog = null;
if (!(EventLog.SourceExists("WebLog"))){ 
EventLog.CreateEventSource("WebLog", "MyLog");

}
if (eventLog == null){
eventLog = new EventLog("MyLog");
eventLog.Source = "WebLog";

} 
eventLog.WriteEntry("This is error!", EventLogEntryType.Error);
浙公网安备 33010602011771号