我使用 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".
要查找和更改此政策,请执行以下操作(或通过其他同等方式执行):
Windows 键
+ 点按R
。- 复制并粘贴:
%windir%\system32\secpol.msc/s
。 - 点击
确定
或按Enter
。 - 导航至:
本地策略\安全选项
。查找条目:用户帐户控制:将文件和注册表写入失败虚拟化到每个用户位置
。 - 此设置是否设为“
已激活
”?
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:
- Bitness: Program must be 32bit.
- Access Rights: Must not run with administrator rights.
- 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 的内容):
它写在哪里?:如果此数据重定向/虚拟化设置处于事件状态,日志将自动重定向到可写位置,同时仍显示原始路径应用。请检查:%LocalAppData%\VirtualStore
。程序:
Windows 键
+ 点按R
。- 复制并粘贴:
%LocalAppData%\VirtualStore
。 - 点击
确定
或按Enter
。 - 检查子文件夹(如果有)。
Solution: This problem can be dealt with in several ways.
Write Location: Write to a writable location outright - by design. This is the only sane approach for the future in my opinion.
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.
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).
技术说明:仅供记录。关于我用来调试的测试应用程序:
- 标准 Windows 窗体 C# 应用程序。
- 在按钮单击事件上或直接在主函数中:
File.WriteAllText(@"C:\Program Files\Myfolder\TestFile.txt", "Test");
(创建路径首先将“Program Files”部分调整为您的语言) 项目
=>添加新项目...
=>应用程序 list 文件
。- 注释掉整个
“Security”
部分。 - 编译 32 位可执行文件。
现在尝试构建并运行。您应该在任务管理器的UAC-虚拟化列中看到为虚拟化激活的应用程序(上面的屏幕截图)。如果数据重定向/虚拟化策略处于事件状态,则单击该按钮应写入 %LocalAppData%\VirtualStore
。
一些链接:
- System.UnauthorizedAccessException while running .exe under program files
- Please explain VirtualStore for non-experts
- Inside Windows Vista User Account Control - 马克·鲁西诺维奇
- Force an existing application to always run with UAC virtualization on
- 修补问题:UAC Virtualization – Allowing standard users to update a system protected area
关于windows-installer - log4net 日志文件在应用程序安装子文件夹中的 Windows 资源管理器中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54040854/