Wix Windows服务日志记录
我有一个Windows服务,我想用NLog记录一些信息。我的NLog文件如下所示:Wix Windows服务日志记录,wix,windows-services,nlog,wix3.7,appdata,Wix,Windows Services,Nlog,Wix3.7,Appdata,我有一个Windows服务,我想用NLog记录一些信息。我的NLog文件如下所示: <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <time type="AccurateUtc" /&
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<time type="AccurateUtc" />
<targets>
<target name="debug-logfile" xsi:type="File" fileName="${specialfolder:folder=ApplicationData}/log/${date:format=yyyy-MM}.log" layout="${longdate} | ${level} | ${message}" />
<target name="trace-logfile" xsi:type="File" fileName="${specialfolder:folder=ApplicationData}/log/trace/${date:format=yyyy-MM-dd}.log" layout="${date:format=yyyy-MM-dd HH\:mm\:ss.fffffff} | ${level} | ${message}" />
<target name="errorfile" xsi:type="File" fileName="${specialfolder:folder=ApplicationData}/log/Error.log" layout="${longdate} | ${level} | ${message}" />
<target name="console" xsi:type="Console" layout="${longdate} | ${level} | ${callsite} | ${message}"/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="trace-logfile" />
<logger name="*" minlevel="Debug" writeTo="debug-logfile" />
<logger name="*" minlevel="Error" writeTo="errorfile" />
</rules>
</nlog>
编辑
使用installutil.exe,服务将其日志写入:
C:\Windows\ServiceProfiles\LocalService\AppData\Roaming\ATLED\log\trace
我是NLog新手。但当我将Log4Net与windows服务一起使用时,我也遇到了类似的问题。我正在从代码中手动设置“appdata”路径。NLog中必须有一种方法可以从代码中手动设置此路径
但是,当windows服务按照您提到的方式运行时,它将采用您提到的“appdata”路径。
为了克服这个问题,我使用了WMI windows API。请参考下面的例子
这里的想法是从WMI API获取用户名并手动构造路径
private string GetWindowsUserAccountName()
{
string userName = string.Empty;
ManagementScope ms = new ManagementScope("\\\\.\\root\\cimv2");
ObjectQuery query = new ObjectQuery("select * from win32_computersystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(ms, query);
foreach (ManagementBaseObject mo in searcher?.Get())
{
userName = mo["username"]?.ToString();
}
userName = userName?.Substring(userName.IndexOf(@"\",StringComparison.InvariantCulture) + 1);
FCLogger.Logger.LogDebug("[ReturnWindowsUserAccountName]UserName Fetched:" + userName);
return userName;
}
您可以自己构造路径,因为这里只有用户名是未知字段
var userName = GetWindowsUserAccountName();
var appDataPath = Path.Combine(LoggerConstants.C, LoggerConstants.USERS, userName, LoggerConstants.APP_DATA, LoggerConstants.ROAMING);
//Ex: "C:\\Users\\" + userName from WMI +"\\AppData"+"\\Roaming" ;
您正在查找
${specialfolder:folder=LocalApplicationData}
?问题不是从第二个程序获取服务的用户。问题是,用户被设置为“本地系统”,但必须是“本地服务”-这样就可以了。但是,如果您希望将该服务作为本地服务安装,在许多情况下,windows服务作为本地服务运行,这样您就可以访问特定于用户的配置文件。
var userName = GetWindowsUserAccountName();
var appDataPath = Path.Combine(LoggerConstants.C, LoggerConstants.USERS, userName, LoggerConstants.APP_DATA, LoggerConstants.ROAMING);
//Ex: "C:\\Users\\" + userName from WMI +"\\AppData"+"\\Roaming" ;