【问题标题】:WCF Windows Service does not log to Event Logs when hosted.WCF Windows 服务在托管时不会记录到事件日志。
【发布时间】:2013-08-13 11:50:42
【问题描述】:

我有一个作为 Windows 服务托管的 WCF 类库。问题是当我在调试模式下将服务作为控制台应用程序运行时,它会正确记录到事件日志中。但是,当我使用我用 inno setup 创建的安装文件将它作为 Windows 服务托管时,由于某种原因它不会记录任何内容。

<system.diagnostics>
<switches>
  <add name="Retail.ReaderService.Switch" value="4" />
</switches>
<trace autoflush="false" indentsize="4">
  <listeners>
    <add name="EventLogTraceListener" type="System.Diagnostics.EventLogTraceListener" initializeData="ServiceLog" />
  </listeners>
</trace>
</system.diagnostics>

我在 app.config 主文件中有这些设置。我尝试将 autoflush 属性更改为 true,但它不起作用。请帮忙。

谢谢,

【问题讨论】:

    标签: .net wcf rest c#-4.0 windows-services


    【解决方案1】:

    当您在调试模式下运行程序时,它会像您一样运行,Govs。如果您在计算机上的管理员组中,则该程序以管理员身份运行,并且它有权读取/写入事件日志。但是,当您将该程序作为 Windows 服务运行时,它仅具有分配给该用户帐户的权限,而该用户帐户似乎没有写入事件日志的权限。

    我遇到了同样的问题,写了这个函数来检测我是否有权写入事件日志。

    private void GetServicePermissionLevel()
    {
    bool bAdmin = false;
    try {
        SecurityIdentifier sidAdmin = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
        AppDomain myDomain = Thread.GetDomain();
        myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
        WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
        if (myPrincipal.IsInRole(sidAdmin)) {
            bAdmin = true;
        } else {
            bAdmin = false;
        }
    } catch (Exception ex) {
        throw new Exception("Error in GetServicePermissionlevel(): " + ex.Message + " - " + ex.StackTrace);
    } finally {
        _ServiceRunAsAdmin = bAdmin;
    }
    }
    

    然后当我想做一些事件记录时,我使用这个:

    if (_ServiceRunAsAdmin)
    EventLog.WriteEntry(c_ServiceName, "Error in GetServiceHostParamSet: " + ex.Message + ". This parameter not found: " + sName);
    

    希望这会有所帮助。

    【讨论】:

    • 嗨,Brian,我正在使用 trace.writeline 来编写日志。你认为它也会有同样的问题吗?
    • 其实我是电脑的管理员。所以我认为这不是问题。还有什么想法吗?
    • 正是......当您在调试模式下运行程序时,我们以管理员身份运行的程序,因此有权读取/写入事件日志。但是,您在其下运行程序的 Windows 服务帐户呢?可能没有管理员权限。我包含的代码将从程序运行的用户帐户中找出具有管理员权限的用户帐户。因为没有管理员权限,您无法写入事件日志。
    • 如果我读到这个权利,这段代码不只是检查你是否有权利,但实际上并不能帮助解决它不写的问题吗?
    【解决方案2】:

    我遇到了同样的问题。解决方案是确保适用于 WCF 应用程序的系统诊断设置也在 Windows 服务的 app.config 中设置。我一复制我的日志就出现了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多