Window 2008 R2 软件限制策略的默认调整,导致记录事件日志的权限不足

我电脑升级成 Window 2008 R2 后, 一个企业服务的项目出现如下错误:未找到源,但未能搜索某些或全部事件日志。不可访问的日志: Security。

在这个企业服务中,当有错误发生时候,会把错误记录到Windows的事件日志中,这部分的代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace CSDN.Community.TopicListDataCenter
{
    /// <summary>
    /// </summary>记录到事件日志
    /// 
    public class MyLog
    {

        /// <summary>
        /// </summary>把异常写入日志
        /// 
        /// 
        /// 
        /// public static void Writer(string group, string name, Exception ex)
        {
            if (GACGlobals.IsNullorEmpty(group))
            {
                MyLog.Writer(ex);
                return;
            }


            StringBuilder text = new StringBuilder();
            if (!GACGlobals.IsNullorEmpty(name))
                text.AppendLine(name);
            text.AppendFormat("{0} : {1}", DateTime.Now.ToString("u"), Environment.NewLine);

            if (ex != null)
            {
                text.AppendFormat("\t   Type: {0}{1}", ex.GetType().Name, Environment.NewLine);
                text.AppendFormat("\tMessage: {0}{1}", ex.Message, Environment.NewLine);
                text.AppendFormat("\t Source: {0}{1}", ex.Source, Environment.NewLine);
                text.AppendFormat("\t  Stack: {0}{1}", ex.StackTrace, Environment.NewLine);

                while (ex.InnerException != null)
                {
                    text.AppendFormat("\t   Type: {0}{1}", ex.InnerException.GetType().Name, Environment.NewLine);
                    text.AppendFormat("\tMessage: {0}{1}", ex.InnerException.Message, Environment.NewLine);
                    text.AppendFormat("\t Source: {0}{1}", ex.InnerException.Source, Environment.NewLine);
                    text.AppendFormat("\t  Stack: {0}{1}", ex.InnerException.StackTrace, Environment.NewLine);
                    ex = ex.InnerException;
                }
            }

            text.AppendLine("***********************");

            // 写事件日志
            if (!EventLog.SourceExists(group))
            {
                EventLog.CreateEventSource(group, "Application");
            }

            EventLog logEntry = new EventLog();
            logEntry.Source = group;
            logEntry.WriteEntry(text.ToString(), EventLogEntryType.Error);
            logEntry.Close();
        }


        /// <summary>
        /// </summary>把异常写入时间日志
        /// 
        /// public static void Writer(Exception ex)
        {

            StringBuilder text = new StringBuilder();
            text.AppendFormat("{0} : {1}", DateTime.Now.ToString("u"), Environment.NewLine);

            if (ex != null)
            {
                text.AppendFormat("\t   Type: {0}{1}", ex.GetType().Name, Environment.NewLine);
                text.AppendFormat("\tMessage: {0}{1}", ex.Message, Environment.NewLine);
                text.AppendFormat("\t Source: {0}{1}", ex.Source, Environment.NewLine);
                text.AppendFormat("\t  Stack: {0}{1}", ex.StackTrace, Environment.NewLine);

                while (ex.InnerException != null)
                {
                    text.AppendFormat("\t   Type: {0}{1}", ex.InnerException.GetType().Name, Environment.NewLine);
                    text.AppendFormat("\tMessage: {0}{1}", ex.InnerException.Message, Environment.NewLine);
                    text.AppendFormat("\t Source: {0}{1}", ex.InnerException.Source, Environment.NewLine);
                    text.AppendFormat("\t  Stack: {0}{1}", ex.InnerException.StackTrace, Environment.NewLine);
                    ex = ex.InnerException;
                }
            }

            text.AppendLine("***********************");

            // 写事件日志
            if (!EventLog.SourceExists(".NET Runtime"))
            {
                EventLog.CreateEventSource(".NET Runtime", "Application");
            }

            EventLog logEntry = new EventLog();
            logEntry.Source = ".NET Runtime";
            logEntry.WriteEntry(text.ToString(), EventLogEntryType.Error);
            logEntry.Close();
        }

    }
}

显然,默认情况下,是因为无权执行 EventLog.CreateEventSource( 导致的问题,

而这个问题,以前在 Win2008, Win2003 上都是没有问题的。

以前我这里的设置如下图:

image

在Win2008 R2 中如果要解决这个问题,则需要 打开 应用软件限制策略,并把它设置成“不受限”即可。这样就有权限执行 EventLog.CreateEventSource( 这行代码了。如下图:

image

或者,手工在注册表中提前添加 EventLog.CreateEventSource( 用到的一些日志分类。
即:在注册表里预先建好日志项(运行regedit,HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog
右键单击新建-->项)

在上述设置选项中,除了不受限,不允许外,还有一个名为”基本用户”的策略,”基本用户”这条策略的含义是,除了通过其他规则明确指定的软件外,其他所有软件都只能以一般用户的身份运行(也就是说,不能以管理员的身份运行)。

参考资料:

C# 编写的Windows serice程序. 安装时出现异常
http://topic.csdn.net/u/20080129/16/479cedb2-d848-422a-a53a-5402e4384b20.html

COM+ 应用程序 属性页:“安全”选项卡
http://technet.microsoft.com/zh-cn/library/dd145616.aspx

活用软件限制策略
http://www.xieyidian.com/?p=266

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值