Skip to content

Commit

Permalink
优化在win10以及同时代系统下处理windows目录下的权限问题,会导致无法记录日志
Browse files Browse the repository at this point in the history
  • Loading branch information
hp-notebook committed Jul 15, 2020
1 parent 482f84d commit 3e0617e
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 15 deletions.
4 changes: 2 additions & 2 deletions AnyExeToService/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyVersion("1.0.2.0")]
[assembly: AssemblyFileVersion("1.0.2.0")]
Binary file modified AnyExeToService/Resources/ServiceApp.exe
Binary file not shown.
2 changes: 2 additions & 0 deletions ServiceApp/ExeProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void StartProcess()
{
_mainProcess?.Kill();
_mainProcess?.Dispose();
_log.Debug($"程序路径:{_config.Exe}");
_log.Debug($"启动参数:{_config.Argument}");
_mainProcess = new Process
{
StartInfo = new ProcessStartInfo(_config.Exe)
Expand Down
18 changes: 14 additions & 4 deletions ServiceApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using log4net;

namespace ServiceApp
Expand All @@ -11,12 +12,19 @@ class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(Properties.Resources.log4net1)))
var processName = Process.GetCurrentProcess().ProcessName;
var dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), processName);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
Trace.WriteLine($"当前正在使用的日志路径为:{dir}");
var logConfig = $"{dir}\\{processName}.log.config";
if (!File.Exists(logConfig))
{
log4net.Config.XmlConfigurator.Configure(ms);
File.WriteAllText(logConfig, Properties.Resources.log4net1.Replace("$DIR$", dir).Replace("\\", "\\\\"));
}

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
log4net.Config.XmlConfigurator.Configure(new Uri(logConfig));

var log = LogManager.GetLogger(typeof(Program));
try
{
Expand All @@ -33,6 +41,8 @@ private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEven
{
if (args.Name.Contains("Newtonsoft.Json"))
return Assembly.Load(Properties.Resources.Newtonsoft_Json);
if (args.Name.Contains("log4net"))
return Assembly.Load(Properties.Resources.log4net_dll);
return null;
}
}
Expand Down
4 changes: 2 additions & 2 deletions ServiceApp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyVersion("1.0.2.0")]
[assembly: AssemblyFileVersion("1.0.2.0")]
16 changes: 13 additions & 3 deletions ServiceApp/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ServiceApp/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@
<data name="log4net1" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\log4net.config;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
<data name="log4net_dll" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\log4net.dll.dat;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="Newtonsoft_Json" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\Newtonsoft.Json.dll;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
Expand Down
File renamed without changes.
4 changes: 1 addition & 3 deletions ServiceApp/ServiceApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Include="Resources\log4net.dll" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\log4net.dll.dat" />
<None Include="Resources\Newtonsoft.Json.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
2 changes: 1 addition & 1 deletion ServiceApp/log4net.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</configSections>
<log4net>
<appender name="RootAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Logs\\" />
<param name="File" value="$DIR$\\" />
<param name="AppendToFile" value="true" />
<param name="MaxFileSize" value="10240" />
<param name="MaxSizeRollBackups" value="100" />
Expand Down

0 comments on commit 3e0617e

Please sign in to comment.