diff --git a/.gitignore b/.gitignore index 8f8d5b6..cf1afee 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ obj bin /packages /.vs +/MailSend.Web/Temp diff --git a/MailSend.Common/Job/ClearTempJob.cs b/MailSend.Common/Job/ClearTempJob.cs new file mode 100644 index 0000000..191e28c --- /dev/null +++ b/MailSend.Common/Job/ClearTempJob.cs @@ -0,0 +1,55 @@ +using log4net; +using Quartz; +using System; +using System.Collections.Generic; +using System.Configuration; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using System.Web; + +namespace MailSend.Common +{ + /// + /// 清理临时文件 + /// + public class ClearTempJob : IJob + { + private ILog logger = LogManager.GetLogger("ClearTempJob"); + + Task IJob.Execute(IJobExecutionContext context) + { + return Task.Run(() => + { + var tmpDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Temp"); + var clearCount = 0; + if (Directory.Exists(tmpDir)) + { + var files = Directory.GetFiles(tmpDir); + foreach (string file in files) + { + var fi = new FileInfo(file); + if (fi.LastAccessTime < DateTime.Now.AddDays(-1)) + { + try + { + File.Delete(file); + clearCount++; + } + catch (Exception ex) + { + logger.Error(ex.Message); + logger.Error(ex.StackTrace); + } + } + } + } + + if (clearCount > 0) + { + logger.Debug($"成功清理临时文件{clearCount}个"); + } + }); + } + } +} \ No newline at end of file diff --git a/MailSend.Common/Job/LogZipJob.cs b/MailSend.Common/Job/LogZipJob.cs new file mode 100644 index 0000000..d18c54b --- /dev/null +++ b/MailSend.Common/Job/LogZipJob.cs @@ -0,0 +1,68 @@ +using log4net; +using Quartz; +using System; +using System.Collections.Generic; +using System.Configuration; +using System.IO; +using System.Linq; +using System.Web; +using ICSharpCode.SharpZipLib.Zip; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace MailSend.Common +{ + /// + /// 压缩日志 + /// + public class LogZipJob : IJob + { + private ILog logger = LogManager.GetLogger("LogZipJob"); + + Task IJob.Execute(IJobExecutionContext context) + { + return Task.Run(() => + { + logger.Debug($"准备压缩日志文件"); + string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data\\Logs"); + logger.Debug(logPath); + string[] files = Directory.GetFiles(logPath); + string regex = "^(debug|error)\\.log(\\.[0-9]{1,3})?$"; + if (files.Length > 0) + { + string zipFile = Path.Combine(logPath, $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.zip"); + string tempPath = Path.Combine(logPath, "temp"); + if (Directory.Exists(tempPath)) + { + Directory.Delete(tempPath, true); + } + Directory.CreateDirectory(tempPath); + + try + { + foreach (string file in files) + { + FileInfo fileInfo = new FileInfo(file); + if (!Regex.IsMatch(fileInfo.Name, regex) && fileInfo.Extension != ".zip") + { + logger.Debug($"发现待压缩文件:{fileInfo.Name}"); + + File.Copy(file, Path.Combine(tempPath, fileInfo.Name)); + File.Delete(file); + } + } + FastZip fastZip = new FastZip(); + fastZip.CreateZip(zipFile, tempPath, true, ""); + Directory.Delete(tempPath, true); + } + catch (Exception ex) + { + logger.Error(ex.Message); + logger.Error(ex.StackTrace); + } + } + logger.Debug($"压缩日志文件完成"); + }); + } + } +} \ No newline at end of file diff --git a/MailSend.Common/MailSend.Common.csproj b/MailSend.Common/MailSend.Common.csproj index 6e9b438..355100d 100644 --- a/MailSend.Common/MailSend.Common.csproj +++ b/MailSend.Common/MailSend.Common.csproj @@ -40,16 +40,26 @@ ..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.SqlServer.dll + + ..\packages\SharpZipLib.1.3.3\lib\net45\ICSharpCode.SharpZipLib.dll + ..\packages\log4net.2.0.14\lib\net45\log4net.dll + + ..\packages\Microsoft.Extensions.Logging.Abstractions.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll + ..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll + + ..\packages\Quartz.3.3.3\lib\net461\Quartz.dll + + @@ -60,6 +70,8 @@ + + diff --git a/MailSend.Common/packages.config b/MailSend.Common/packages.config index 437e26c..d2b30d3 100644 --- a/MailSend.Common/packages.config +++ b/MailSend.Common/packages.config @@ -2,5 +2,8 @@ + + + \ No newline at end of file diff --git a/MailSend/App.config b/MailSend/App.config index c3c43fa..71c74d6 100644 --- a/MailSend/App.config +++ b/MailSend/App.config @@ -43,8 +43,6 @@ - diff --git a/MailSend/MailSend.Service.csproj b/MailSend/MailSend.Service.csproj index 06007b3..a28fafe 100644 --- a/MailSend/MailSend.Service.csproj +++ b/MailSend/MailSend.Service.csproj @@ -15,6 +15,21 @@ true + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true AnyCPU @@ -123,6 +138,18 @@ MailSend.Common + + + False + Microsoft .NET Framework 4.6.1 %28x86 和 x64%29 + true + + + False + .NET Framework 3.5 SP1 + false + + diff --git a/MailSend/MailSend.Service.csproj.user b/MailSend/MailSend.Service.csproj.user new file mode 100644 index 0000000..6513a72 --- /dev/null +++ b/MailSend/MailSend.Service.csproj.user @@ -0,0 +1,16 @@ + + + + true + + + publish\ + + + + + + zh-CN + false + + \ No newline at end of file diff --git a/MailSend/quartz_jobs.xml b/MailSend/quartz_jobs.xml index 507265f..14fcbc9 100644 --- a/MailSend/quartz_jobs.xml +++ b/MailSend/quartz_jobs.xml @@ -28,12 +28,12 @@ - LogZipJob System 压缩日志 - DSWeb.Job.Common.LogZipJob,DSWeb.Job.Common + MailSend.Common.LogZipJob,MailSend.Common true false @@ -46,8 +46,28 @@ System 0 30 0 * * ? - --> + + + + + ClearTempJob + System + 清理临时文件 + MailSend.Common.ClearTempJob,MailSend.Common + true + false + + + + ClearTempJobScheduler + System + ClearTempJob + System + -1 + 1800000 + + \ No newline at end of file