1
1
using DotnetSpider . Core . Infrastructure ;
2
2
using Microsoft . Extensions . Logging ;
3
3
using Serilog ;
4
- using Serilog . Sinks . SystemConsole . Themes ;
5
4
using System ;
6
- using System . Collections . Generic ;
7
5
using System . Threading . Tasks ;
8
6
9
7
namespace DotnetSpider . Core
@@ -13,27 +11,6 @@ namespace DotnetSpider.Core
13
11
/// </summary>
14
12
public abstract class AppBase : Named , IAppBase
15
13
{
16
- public static SystemConsoleTheme ConsoleLogTheme { get ; set ; } = new SystemConsoleTheme (
17
- new Dictionary < ConsoleThemeStyle , SystemConsoleThemeStyle >
18
- {
19
- [ ConsoleThemeStyle . Text ] = new SystemConsoleThemeStyle { Foreground = ConsoleColor . Gray } ,
20
- [ ConsoleThemeStyle . SecondaryText ] = new SystemConsoleThemeStyle { Foreground = ConsoleColor . DarkGray } ,
21
- [ ConsoleThemeStyle . TertiaryText ] = new SystemConsoleThemeStyle { Foreground = ConsoleColor . DarkGray } ,
22
- [ ConsoleThemeStyle . Invalid ] = new SystemConsoleThemeStyle { Foreground = ConsoleColor . Yellow } ,
23
- [ ConsoleThemeStyle . Null ] = new SystemConsoleThemeStyle { Foreground = ConsoleColor . White } ,
24
- [ ConsoleThemeStyle . Name ] = new SystemConsoleThemeStyle { Foreground = ConsoleColor . White } ,
25
- [ ConsoleThemeStyle . String ] = new SystemConsoleThemeStyle { Foreground = ConsoleColor . White } ,
26
- [ ConsoleThemeStyle . Number ] = new SystemConsoleThemeStyle { Foreground = ConsoleColor . White } ,
27
- [ ConsoleThemeStyle . Boolean ] = new SystemConsoleThemeStyle { Foreground = ConsoleColor . White } ,
28
- [ ConsoleThemeStyle . Scalar ] = new SystemConsoleThemeStyle { Foreground = ConsoleColor . White } ,
29
- [ ConsoleThemeStyle . LevelVerbose ] = new SystemConsoleThemeStyle { Foreground = ConsoleColor . Cyan } ,
30
- [ ConsoleThemeStyle . LevelDebug ] = new SystemConsoleThemeStyle { Foreground = ConsoleColor . DarkGray } ,
31
- [ ConsoleThemeStyle . LevelInformation ] = new SystemConsoleThemeStyle { Foreground = ConsoleColor . White } ,
32
- [ ConsoleThemeStyle . LevelWarning ] = new SystemConsoleThemeStyle { Foreground = ConsoleColor . Yellow } ,
33
- [ ConsoleThemeStyle . LevelError ] = new SystemConsoleThemeStyle { Foreground = ConsoleColor . Red } ,
34
- [ ConsoleThemeStyle . LevelFatal ] = new SystemConsoleThemeStyle { Foreground = ConsoleColor . Red }
35
- } ) ;
36
-
37
14
private string _identity = Guid . NewGuid ( ) . ToString ( "N" ) ;
38
15
private ILoggerFactory _loggerFactory ;
39
16
@@ -54,11 +31,12 @@ public string Identity
54
31
55
32
if ( string . IsNullOrWhiteSpace ( value ) )
56
33
{
57
- throw new ArgumentException ( $ "{ nameof ( Identity ) } should not be empty or null. ") ;
34
+ throw new ArgumentException ( $ "{ nameof ( Identity ) } should not be empty or null") ;
58
35
}
36
+
59
37
if ( value . Length > Env . IdentityMaxLength )
60
38
{
61
- throw new ArgumentException ( $ "Length of identity should less than { Env . IdentityMaxLength } . ") ;
39
+ throw new ArgumentException ( $ "Length of identity should less than { Env . IdentityMaxLength } ") ;
62
40
}
63
41
64
42
_identity = value ;
@@ -73,12 +51,12 @@ public string Identity
73
51
/// <summary>
74
52
/// start time of spider.
75
53
/// </summary>
76
- protected DateTime StartTime { get ; private set ; }
54
+ public DateTime StartTime { get ; private set ; }
77
55
78
56
/// <summary>
79
57
/// end time of spider.
80
58
/// </summary>
81
- protected DateTime ExitTime { get ; private set ; }
59
+ public DateTime ExitTime { get ; private set ; }
82
60
83
61
/// <summary>
84
62
/// 运行记录接口
@@ -115,7 +93,7 @@ public void Run(params string[] arguments)
115
93
116
94
var loggerConfiguration = new LoggerConfiguration ( )
117
95
. MinimumLevel . Verbose ( )
118
- . WriteTo . Console ( theme : ConsoleLogTheme )
96
+ . WriteTo . Console ( theme : SerilogConsoleTheme . ConsoleLogTheme )
119
97
. WriteTo . RollingFile ( "dotnetspider.log" ) ;
120
98
121
99
if ( Env . HubServiceLog )
@@ -131,14 +109,17 @@ public void Run(params string[] arguments)
131
109
132
110
if ( ExecuteRecord == null )
133
111
{
134
- ExecuteRecord = string . IsNullOrWhiteSpace ( Env . HubServiceUrl ) ? ( IExecuteRecord ) new NullExecuteRecord ( ) : new HttpExecuteRecord ( ) ;
112
+ ExecuteRecord = string . IsNullOrWhiteSpace ( Env . HubServiceUrl )
113
+ ? ( IExecuteRecord ) new NullExecuteRecord ( )
114
+ : new HttpExecuteRecord ( ) ;
135
115
ExecuteRecord . Logger = Logger ;
136
116
}
137
117
138
118
if ( ! ExecuteRecord . Add ( TaskId , Name , Identity ) )
139
119
{
140
- Logger . LogError ( $ "Add execute record: { Identity } failed. ") ;
120
+ Logger . LogError ( $ "Add execute record: { Identity } failed") ;
141
121
}
122
+
142
123
try
143
124
{
144
125
StartTime = DateTime . Now ;
@@ -148,7 +129,7 @@ public void Run(params string[] arguments)
148
129
{
149
130
ExitTime = DateTime . Now ;
150
131
ExecuteRecord . Remove ( TaskId , Name , Identity ) ;
151
- Logger . LogInformation ( $ "Consume: { ( ExitTime - StartTime ) . TotalSeconds } seconds. ") ;
132
+ Logger . LogInformation ( $ "Consume: { ( ExitTime - StartTime ) . TotalSeconds } seconds") ;
152
133
PrintInfo . PrintLine ( ) ;
153
134
}
154
135
}
@@ -159,4 +140,4 @@ public void Run(params string[] arguments)
159
140
160
141
public abstract void Exit ( Action action = null ) ;
161
142
}
162
- }
143
+ }
0 commit comments