在ClickOnce应用程序事件日志使用 [英] Using EventLog in ClickOnce application

查看:245
本文介绍了在ClickOnce应用程序事件日志使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了我在多个ClickOnce应用程序使用的库。在这个库中的错误时,我想错误写入Windows 事件日志

I've got a library that I use across multiple ClickOnce applications. In the event of an error in this library I would like to write the error to the windows EventLog.

我发现如何,但似乎一个知识库文章这需要管理员权限,以搜索为源。特别是,它扼流圈试图搜索安全事件日志的时候。

I found a KB article on how but it seems that this requires administrator permissions to search the for the source. Specifically it chokes when trying to search the Security event log.

反正是有解决这个和写入事件日志中ClickOnce应用程序?我看见一个人试图写一个已知源,但他们似乎没有能够找到一个来源, 。是始终可用。

Is there anyway to work around this and write to the event log in a ClickOnce application? I saw one person trying to write to a known source, but they didn't seem to be able to find a source that was consistently available.

编辑:根据

这答案在这里,我创建这是包括我的应用程序,我可以在第一次运行运行设置事件源,可以得到管理员权限。但是一旦源创建看来我还是不能写入它

Based on answers here I create an program that's included with my application that I can run on the first run to set up the event source that can get admin privileges. However once the source is created it seems I still cannot write to it.

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        if (!EventLog.SourceExists("ATE"))
        {
            EventLog.CreateEventSource("ATE", "Application");
        }
    }



时正确创建源(它等同于通过雅尼克布隆提供的注册表编辑)。当我写在我的非提升应用程序的源我收到一个新的错误,但它仍然无法正常工作。新的错误是:

Is properly creates a source (which is equivalent to the registry edit provided by Yannick Blondeau). When I write to the source in my non-elevated application I receive an new error, but it still doesn't work. The new error is:

无法打开日志源'ATE。您可能没有写访问

编辑2:

我现在已经在试图得到它的工作通过注册表编辑在 CustomSD键。我尝试添加(A ;;为0x7 ;;; AU)给身份验证的用户完全访问权限,但它似乎没有任何效果。

I've now been trying to get it to work through registry edits on the CustomSD key. I tried adding (A;;0x7;;;AU) to give authenticated users full access but it didn't seem to have any effect.

推荐答案

不幸的是,事件源要求创建管理priveledges。不过,你不需要管理员权限写入事件日志,或从中读取。

Unfortunately, the event source requires administrative priveledges to be created. However, you don't need admin rights to write to the event log, or read from it.

有解决这个两个方法。

在安装该应用程序作为管理员,您添加新的事件源。

You add the new event source when you install the application as an administrator.

您创建运行作为一个管理员配置应用程序一个简单的应用程序。这甚至可以包含在安装程序。

You create a simple app that you run as an admin to configure your application. This could even be included in the installer.

如果你没有或者想要安装程序,负载应用到计算机作为管理员,一旦运行该程序。您的应用程序启动时,应配置事件源,如果它已不存在,无论如何,对不对? (好吧,这三种方式。)

If you don't have or want an installer, the load the app onto the computer as an admin and run the program once. Your app startup should configure the event source if it isn't already there anyway, right? (Okay, that's three ways.)

这代码来自微软:的http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx 并设计为运行作为管理员用户。

This code snippet is from microsoft: http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx and is designed to be run as an admin user.

using System;
using System.Diagnostics;
using System.Threading;

class MySample
{
    public static void Main()
    {
        // Create the source, if it does not already exist. 
        if (!EventLog.SourceExists("MySource"))
        {
             //An event log source should not be created and immediately used. 
             //There is a latency time to enable the source, it should be created 
             //prior to executing the application that uses the source. 
             //Execute this sample a second time to use the new source.
             EventLog.CreateEventSource("MySource", "MyNewLog");
             Console.WriteLine("CreatedEventSource");
             Console.WriteLine("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered. 
            return;
        }
        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = "MySource";

        // Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.");
    }
}



我知道这可能不是你到底是什么后, ,但我认为它是做到这一点的唯一方法。

I know it may not be exactly what you were after, but I reckon it's the only way to do this.

修改1:增加了一些更多的代码

public class EventLogger
{
    private const string logName = "Application";
    private static string appName = "";
    private static bool sourceExists = false;

    public static string AppName
    {
        get { return appName; }
        set { appName = value; }
    }

    public static void Init(string appName)
    {
        AppName = appName;
        sourceExists = EventLog.SourceExists(AppName);

        if (!sourceExists)
        {
            EventLog.CreateEventSource(AppName, logName);
            sourceExists = true;
        }
    }

    private static void Write(string entry, EventLogEntryType logType, int eventID)
    {
        if (sourceExists)
        {
            EventLog.WriteEntry(AppName, entry, logType, eventID);
        }
    }

    public static void Warning(string entry) { Write(entry, EventLogEntryType.Warning, 200); }
    public static void Warning(string entry, int eventID) { Write(entry, EventLogEntryType.Warning, eventID); }
    public static void Error(string entry) { Write(entry, EventLogEntryType.Error, 300); }
    public static void Error(string entry, int eventID) { Write(entry, EventLogEntryType.Error, eventID); }
    public static void Info(string entry) { Write(entry, EventLogEntryType.Information, 100); }
    public static void Info(string entry, int eventID) { Write(entry, EventLogEntryType.Information, eventID); }
}

这是我已经实现了我的事件记录器类中的方法是在使用在生产中的应用。

This is the way that I have implemented my EventLogger class which is in use in a production application.

如果你可以发布您的代码,我们可以做一个比较。

If you could post your code we can do a comparison.

一件事,发生在我的是,当我创建我的源,我用的是应用程序的名称,而是坚持使用应用程序日志文件。你还试图创建一个新的日志文件。如果是这样检查它是否在事件查看器中创建

One thing that occurs to me is that when I create my source, I use the application name, but stick with the Application logfile. Are you also attempting to create a new logfile. If so check that it is created in the event viewer.

修改2:零用户令牌值模拟用户

这是一个有点难倒的的。

This is a bit of a stumper.

试试这个代码,围绕事件写代码包裹。

Try this code, wrapped around the event writing code.

System.Security.Principal.WindowsImpersonationContext wic = System.Security.Principal.WindowsIdentity.Impersonate(IntPtr.Zero);
// your code to write to event log or any to do something which needs elevated permission--
wic.Undo();



我没试过,只是因为我的代码工作,它来自这里:<一HREF =http://sharenotes.wordpress.com/2008/03/18/cannot-open-log-for-source-you-may-not-have-write-access/> HTTP://sharenotes.wordpress .COM / 2008/03/18 /不能打开,日志换源你-MAY-不具备的,写访问/

这篇关于在ClickOnce应用程序事件日志使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆