windows-installer - log4net 日志文件在应用程序安装子文件夹中的 Windows 资源管理器中不可见

标签 windows-installer log4net visual-studio-extensions windows-explorer vdproj

我使用 log4net 作为用 c# 编写的桌面应用程序,并使用 Visual Studio 扩展“Microsoft Visual Studio 安装程序项目”(MSI-Installer) 进行部署。安装后,我没有看到为 log4net 定义的日志子文件夹。

Microsoft Visual Studio 安装程序项目”中的 TARGETDIR 定义为 [ProgramFiles64Folder][Manufacturer]\[ProductName]

log4net日志文件定义为

<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net">
   <param name="File" value=".\log\MyApp.log" />
   <!-- ... -->

通过“Microsoft Visual Studio 安装程序项目”创建的 setup.exe 和 msi-installer 安装应用程序后,我看不到 Windows 资源管理器中 Program Files\MyManufacturer\MyProductName 下的日志文件夹 尽管设置了“显示隐藏文件、文件夹……”。

出于测试目的,我将以下代码添加到我的应用程序中:

// does log file exist and grow ?
string currdir = Directory.GetCurrentDirectory();
MessageBox.Show(currdir, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
string[] sa = File.ReadAllLines(currdir + "\\log\\MyApp.log");
MessageBox.Show(sa.Length.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
// show lines created this year (2019)
string s = "";
for (int i = 0; i < sa.Length; i++)
{
    if(sa[i].Contains("2019")) 
        s = s + sa[i] + "\n";
}
MessageBox.Show(s, "Error", MessageBoxButton.OK, MessageBoxImage.Error);

因此,Program Files\MyManufacturer\MyProductName\log\MyApp.log下的日志文件 可以通过应用程序访问,我看到它在增长!

所以问题是:日志子目录在哪里?如何使其在 Windows 资源管理器中可见? 这是权限问题吗?

最佳答案

Short Version: Please see if you have any virtualized folders under: %LocalAppData%\VirtualStore.

If you see a redirected file written here, then you are seeing the result of redirected file writes due to lack of permissions to write to the original location. It is a policy controlled feature with a number of snags to be aware of. Details below.


Bitness?:您确定正在查找正确的文件夹吗?您是否检查过 Program Files (x86) 来确定?


虚拟化/数据重定向:我想知道您是否启用了策略,可以自动将安全文件夹中的失败写入重定向到另一个可写位置? User Account Control: Virtualize file and registry write failures to per-user locations .

数据重定向/虚拟化策略条目:

Note: The local security policy applet is available only in professional, enterprise and corporate editions of Windows. It will be lacking in "home editions".

要查找和更改此政策,请执行以下操作(或通过其他同等方式执行):

  1. Windows 键 + 点按 R
  2. 复制并粘贴:%windir%\system32\secpol.msc/s
  3. 点击确定或按Enter
  4. 导航至:本地策略\安全选项。查找条目:用户帐户控制:将文件和注册表写入失败虚拟化到每个用户位置
  5. 此设置是否设为“已激活”?

Prerequisite Conditions: Even if this setting is set to activated, redirection only works under very specific circumstances. For a program to be eligible for virtualization it must comply as follow:

  1. Bitness: Program must be 32bit.
  2. Access Rights: Must not run with administrator rights.
  3. Manifest: Must not have been compiled with a manifest file (indicating it is for Vista or later). Or you comment out the security section.

(see below for technical details on how to create a test application). All of this from this source. And here is another SO answer.

任务管理器和虚拟化:您可以通过添加虚拟化状态来查看程序的>虚拟化列到任务管理器中的进程页面(在 Windows 10 中使用“详细信息” View - 那里应该显示类似 UAC-Virtualization 的内容):

Task Manager UAC-Virtualization

它写在哪里?:如果此数据重定向/虚拟化设置处于事件状态,日志将自动重定向到可写位置,同时仍显示原始路径应用。请检查:%LocalAppData%\VirtualStore。程序:

  1. Windows 键 + 点按 R
  2. 复制并粘贴:%LocalAppData%\VirtualStore
  3. 点击确定或按Enter
  4. 检查子文件夹(如果有)。

Solution: This problem can be dealt with in several ways.

  1. Write Location: Write to a writable location outright - by design. This is the only sane approach for the future in my opinion.

  2. ACL Permissions: You can open write permission to the location in question and allow users or everyone write access. I do not like this approach security-wise.

  3. Elevate: I suppose you can run the application with admin rights by using an elevation manifest. I really do not like this approach. Elevated applications are given "the keys to the city" and represent a major security risk.

There are some flavors of these "solutions", but I think I will suggest these three for now. Maybe check the below link(s).


技术说明:仅供记录。关于我用来调试的测试应用程序:

  1. 标准 Windows 窗体 C# 应用程序。
  2. 在按钮单击事件上或直接在主函数中:File.WriteAllText(@"C:\Program Files\Myfolder\TestFile.txt", "Test");(创建路径首先将“Program Files”部分调整为您的语言)
  3. 项目 => 添加新项目... => 应用程序 list 文件
  4. 注释掉整个“Security”部分。
  5. 编译 32 位可执行文件。

现在尝试构建并运行。您应该在任务管理器UAC-虚拟化列中看到为虚拟化激活的应用程序(上面的屏幕截图)。如果数据重定向/虚拟化策略处于事件状态,则单击该按钮应写入 %LocalAppData%\VirtualStore


一些链接:

关于windows-installer - log4net 日志文件在应用程序安装子文件夹中的 Windows 资源管理器中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54040854/

相关文章:

powershell - 尝试让 log4net 与 PowerShell 一起工作(混合使用 log4net 配置文件)

visual-studio-2012 - 多项目解决方案模板

c# - Visual Studio 2017 扩展,用于跨整个解决方案进行自定义 C# 重构

windows-installer - InstallShield:卸载程序时如何删除 MSI 不知道的文件和子文件夹?

log4net - 如何让 log4net 在本地机器上与 Chainsaw 一起工作

nhibernate - 为什么 nhibernate 需要 Log4Net?

visual-studio-2015 - Visual Studio 2015。如何查找具有特定类的较少文件

c# - WiX 安装程序 : Can one msi have - one windows app and one windows service using WiX

Wix Burn Bundle - 必须是管理员

windows - 如何编写自定义操作以在安装完成之前删除已安装的文件