zoukankan      html  css  js  c++  java
  • 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>
            /// <param name="group"></param>
            /// <param name="name"></param>
            /// <param name="ex"></param>
            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>
            /// <param name="ex"></param>
            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

  • 相关阅读:
    QGroundControl编译出错记录
    【Luogu】【关卡2-5】字符串处理(2017年10月)
    【Luogu】【关卡2-4】排序Ex(2017年10月)
    【Luogu】【关卡2-3】排序(2017年10月) 【AK】
    【Luogu】【关卡2-2】交叉模拟(2017年10月)
    【Luogu】【关卡2-1】简单的模拟(2017年10月)
    【基础】图论基础 2017/04/20
    【LeetCode】BFS || DFS [2017.04.10--2017.04.17]
    【LeetCode】排序
    【LeetCode】贪心
  • 原文地址:https://www.cnblogs.com/ghj1976/p/1732388.html
Copyright © 2011-2022 走看看