增加压缩日志调度

master
Hao 3 years ago
parent 2bc0a8f3a9
commit aa7ea7d72a

1
.gitignore vendored

@ -2,3 +2,4 @@ obj
bin
/packages
/.vs
/MailSend.Web/Temp

@ -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
{
/// <summary>
/// 清理临时文件
/// </summary>
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}个");
}
});
}
}
}

@ -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
{
/// <summary>
/// 压缩日志
/// </summary>
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($"压缩日志文件完成");
});
}
}
}

@ -40,16 +40,26 @@
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.SqlServer.dll</HintPath>
</Reference>
<Reference Include="ICSharpCode.SharpZipLib, Version=1.3.3.11, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
<HintPath>..\packages\SharpZipLib.1.3.3\lib\net45\ICSharpCode.SharpZipLib.dll</HintPath>
</Reference>
<Reference Include="log4net, Version=2.0.14.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<HintPath>..\packages\log4net.2.0.14\lib\net45\log4net.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.Logging.Abstractions, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Extensions.Logging.Abstractions.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Quartz, Version=3.3.3.0, Culture=neutral, PublicKeyToken=f6b8c98a402cc8a4, processorArchitecture=MSIL">
<HintPath>..\packages\Quartz.3.3.3\lib\net461\Quartz.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Remoting" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
@ -60,6 +70,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DB\MailDataContext.cs" />
<Compile Include="Job\ClearTempJob.cs" />
<Compile Include="Job\LogZipJob.cs" />
<Compile Include="Models\AttachFileModel.cs" />
<Compile Include="Models\Enums.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

@ -2,5 +2,8 @@
<packages>
<package id="EntityFramework" version="6.4.4" targetFramework="net461" />
<package id="log4net" version="2.0.14" targetFramework="net461" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="2.1.1" targetFramework="net461" />
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net461" />
<package id="Quartz" version="3.3.3" targetFramework="net461" />
<package id="SharpZipLib" version="1.3.3" targetFramework="net461" />
</packages>

@ -43,8 +43,6 @@
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
<add key="quartz.threadPool.threadCount" value="50" />
<add key="quartz.threadPool.threadPriority" value="2" />
<!--<add key="quartz.plugin.xml.type" value="Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz" />
<add key="quartz.plugin.xml.fileNames" value="~/quartz_jobs.xml" />-->
<add key="quartz.jobStore.misfireThreshold" value="30000" />
<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz" />

@ -15,6 +15,21 @@
<Deterministic>true</Deterministic>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@ -123,6 +138,18 @@
<Name>MailSend.Common</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.6.1">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.6.1 %28x86 和 x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<EnableUnmanagedDebugging>true</EnableUnmanagedDebugging>
</PropertyGroup>
<PropertyGroup>
<PublishUrlHistory>publish\</PublishUrlHistory>
<InstallUrlHistory />
<SupportUrlHistory />
<UpdateUrlHistory />
<BootstrapperUrlHistory />
<ErrorReportUrlHistory />
<FallbackCulture>zh-CN</FallbackCulture>
<VerifyUploadedFiles>false</VerifyUploadedFiles>
</PropertyGroup>
</Project>

@ -28,12 +28,12 @@
</trigger>
<!-- 压缩日志 --><!--
<!-- 压缩日志 -->
<job>
<name>LogZipJob</name>
<group>System</group>
<description>压缩日志</description>
<job-type>DSWeb.Job.Common.LogZipJob,DSWeb.Job.Common</job-type>
<job-type>MailSend.Common.LogZipJob,MailSend.Common</job-type>
<durable>true</durable>
<recover>false</recover>
</job>
@ -46,8 +46,28 @@
<job-group>System</job-group>
<cron-expression>0 30 0 * * ?</cron-expression>
</cron>
</trigger>-->
</trigger>
<!-- 清理临时文件 -->
<job>
<name>ClearTempJob</name>
<group>System</group>
<description>清理临时文件</description>
<job-type>MailSend.Common.ClearTempJob,MailSend.Common</job-type>
<durable>true</durable>
<recover>false</recover>
</job>
<trigger>
<simple>
<name>ClearTempJobScheduler</name>
<group>System</group>
<job-name>ClearTempJob</job-name>
<job-group>System</job-group>
<repeat-count>-1</repeat-count>
<repeat-interval>1800000</repeat-interval>
</simple>
</trigger>
</schedule>
</job-scheduling-data>
Loading…
Cancel
Save