1
huanghongfeng
2025-06-06 f5b8c1ae89286dada20ea433ffac84f4c9e72a29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using HslCommunication;
using HslCommunication.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.VisualBasic.FileIO;
using Newtonsoft.Json;
using Quartz;
using SixLabors.ImageSharp.PixelFormats;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.Log;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class LogJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
               
                Task.Run(() =>
                {
                    try
                    {
                        while (true)
                        {
                            WriteLog.GetLog("LogJob").Write($"日志记录:{DateTime.Now}", "LogJob");
                            Run(10);
                            Run2(10);
                            Thread.Sleep(1000 * 10);
                        }
                    }
                    catch { }
                });
 
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(nameof(LogJob) + ":" + ex.Message);
            }
 
            return Task.CompletedTask;
        }
        private static void Run(int saveDays)
        {
            var logPath = System.Environment.CurrentDirectory + "/log";
            if (Directory.Exists(logPath))
            {
                var nowTime = DateTime.Now;
                var dayDir = Directory.GetDirectories(logPath, "20*", System.IO.SearchOption.AllDirectories);
                
                foreach (var dirpath in dayDir)
                {
                    //文件夹名
                    var dirName = Path.GetFileName(dirpath).Replace("_", "-");
 
                    DateTime targetDate = DateTime.ParseExact(dirName, "yyyyMMdd", null);
                    if ((nowTime - targetDate).TotalDays >= saveDays)
                    {
                       
                        //删除文件和文件夹
                        try
                        {
                            var flies = Directory.GetFiles(dirpath);
                            
                            foreach (var item in flies)
                            {
                                File.Delete(item);
                            }
                            Directory.Delete(dirpath);
                        }
                        catch { }
                    }
                }
            }
        }
 
        private static void Run2(int saveDays)
        {
            var logPath = System.Environment.CurrentDirectory + "/txt";
            if (Directory.Exists(logPath))
            {
                var nowTime = DateTime.Now;
                var dayDir = Directory.GetDirectories(logPath, "20*", System.IO.SearchOption.AllDirectories);
 
                foreach (var dirpath in dayDir)
                {
                    //文件夹名
                    var dirName = Path.GetFileName(dirpath).Replace("_", "-");
 
                    DateTime targetDate = DateTime.ParseExact(dirName, "yyyyMMdd", null);
                    if ((nowTime - targetDate).TotalDays >= saveDays)
                    {
 
                        //删除文件和文件夹
                        try
                        {
                            var flies = Directory.GetFiles(dirpath);
 
                            foreach (var item in flies)
                            {
                                File.Delete(item);
                            }
                            Directory.Delete(dirpath);
                        }
                        catch { }
                    }
                }
            }
        }
    }
 
 
}