10
10
using System . Net ;
11
11
using System . Threading ;
12
12
using System . Threading . Tasks ;
13
+ using Pollination ;
13
14
14
15
namespace PollinationSDK
15
16
{
16
17
public static class Helper
17
18
{
18
- public static Serilog . ILogger Logger { get ; set ; } = Serilog . Log . Logger ;
19
+ private static Microsoft . Extensions . Logging . ILogger Logger => LogUtils . GetLogger ( nameof ( Helper ) ) ;
20
+
19
21
public static UserPrivate CurrentUser { get ; set ; }
20
22
21
23
public static UserPrivate GetUser ( )
@@ -67,13 +69,13 @@ public static Project GetAProject(string userName, string projectName)
67
69
// Project not found and person account, create a default demo project.
68
70
if ( e . ErrorCode == 404 && userName == Helper . CurrentUser . Username )
69
71
{
70
- Logger . Information ( $ "Project { projectName } is not found in account { userName } . Now creating this project.") ;
72
+ Logger . Info ( $ "Project { projectName } is not found in account { userName } . Now creating this project.") ;
71
73
var ifPublic = projectName == "demo" ;
72
74
var res = api . CreateProject ( userName , new ProjectCreate ( projectName , _public : ifPublic ) ) ;
73
75
return GetAProject ( userName , projectName ) ;
74
76
}
75
- LogHelper . LogError ( e , $ "Failed to get the project { userName } /{ projectName } ") ;
76
- throw e ;
77
+ Logger . Error ( $ "Failed to get the project { userName } /{ projectName } " + "{e}" , new [ ] { e } ) ;
78
+ throw ;
77
79
}
78
80
79
81
@@ -90,14 +92,14 @@ public static Project GetWritableProject(string projectSlug)
90
92
91
93
public static async Task < bool > UploadDirectoryAsync ( Project project , string directory , Action < int > reportProgressAction = default , CancellationToken cancellationToken = default )
92
94
{
93
- LogHelper . LogInfo ( $ "Uploading a directory { directory } ") ;
94
- LogHelper . LogInfo ( $ "Timeout: { Configuration . Default . Timeout } ") ;
95
+ Logger . Info ( $ "Uploading a directory { directory } ") ;
96
+ Logger . Info ( $ "Timeout: { Configuration . Default . Timeout } ") ;
95
97
96
98
var files = Directory . GetFiles ( directory , "*" , SearchOption . AllDirectories ) ;
97
99
var api = new ArtifactsApi ( ) ;
98
100
99
101
var total = files . Count ( ) ;
100
- LogHelper . LogInfo ( $ "Uploading { total } assets for project { project . Name } ") ;
102
+ Logger . Info ( $ "Uploading { total } assets for project { project . Name } ") ;
101
103
102
104
var finished = 0 ;
103
105
var finishedPercent = 0 ;
@@ -125,7 +127,7 @@ public static async Task<bool> UploadDirectoryAsync(Project project, string dire
125
127
finished += chunk . Count ;
126
128
}
127
129
128
- LogHelper . LogInfo ( $ "Finished uploading assets for project { project . Name } ") ;
130
+ Logger . Info ( $ "Finished uploading assets for project { project . Name } ") ;
129
131
130
132
// canceled by user
131
133
if ( cancellationToken . IsCancellationRequested ) return false ;
@@ -143,7 +145,7 @@ private static async Task<bool> BatchExecute(ArtifactsApi api, Project project,
143
145
// canceled by user
144
146
if ( cancellationToken . IsCancellationRequested )
145
147
{
146
- LogHelper . LogInfo ( $ "Canceled uploading by user") ;
148
+ Logger . Info ( $ "Canceled uploading by user") ;
147
149
break ;
148
150
}
149
151
@@ -152,7 +154,7 @@ private static async Task<bool> BatchExecute(ArtifactsApi api, Project project,
152
154
153
155
if ( finishedTask . IsFaulted || finishedTask . Exception != null )
154
156
{
155
- LogHelper . LogError ( $ "Upload exception: { finishedTask . Exception } ") ;
157
+ Logger . Error ( $ "Upload exception: { finishedTask . Exception } ") ;
156
158
throw finishedTask . Exception ;
157
159
}
158
160
@@ -200,18 +202,18 @@ public static async Task<bool> UploadArtifactAsync(ArtifactsApi api, Project pro
200
202
201
203
restRequest . AddFile ( "file" , filePath ) ;
202
204
203
- LogHelper . LogInfo ( $ "Started upload of { fileRelativePath } ") ;
205
+ Logger . Info ( $ "Started upload of { fileRelativePath } ") ;
204
206
var response = await restClient . ExecuteAsync ( restRequest ) ;
205
207
206
208
if ( response . StatusCode == HttpStatusCode . NoContent )
207
209
{
208
- LogHelper . LogInfo ( $ "Done uploading { fileRelativePath } ") ;
210
+ Logger . Info ( $ "Done uploading { fileRelativePath } ") ;
209
211
return true ;
210
212
}
211
213
else
212
214
{
213
- LogHelper . LogInfo ( $ "Received response code: { response . StatusCode } ") ;
214
- LogHelper . LogInfo ( $ "{ response . Content } ") ;
215
+ Logger . Info ( $ "Received response code: { response . StatusCode } ") ;
216
+ Logger . Info ( $ "{ response . Content } ") ;
215
217
}
216
218
return false ;
217
219
}
@@ -533,13 +535,13 @@ public static async Task<string> DownloadArtifactAsync(ArtifactsApi api, string
533
535
534
536
var url = ( await api . DownloadArtifactAsync ( projOwner , projName , fileRelativePath ) ) ? . ToString ( ) ;
535
537
536
- LogHelper . LogInfo ( $ "Downloading { fileRelativePath } from \n -{ url } \n ") ;
538
+ Logger . Info ( $ "Downloading { fileRelativePath } from \n -{ url } \n ") ;
537
539
// get relative path correct
538
540
saveAsDir = Path . GetDirectoryName ( Path . Combine ( saveAsDir , relativePath ) ) ;
539
541
saveAsDir = Path . GetFullPath ( saveAsDir ) ;
540
542
var path = await Helper . DownloadUrlAsync ( url . ToString ( ) , saveAsDir , reportProgressAction , null , cancelToken ) ;
541
543
542
- LogHelper . LogInfo ( $ "Saved { fileRelativePath } to { path } ") ;
544
+ Logger . Info ( $ "Saved { fileRelativePath } to { path } ") ;
543
545
return path ;
544
546
}
545
547
@@ -551,13 +553,13 @@ public static async Task<string> DownloadArtifactAsync(JobsApi api, string projO
551
553
552
554
var url = ( await api . DownloadJobArtifactAsync ( projOwner , projName , jobId , fileRelativePath , cancelToken ) ) ? . ToString ( ) ;
553
555
554
- LogHelper . LogInfo ( $ "Downloading { fileRelativePath } from \n -{ url } \n ") ;
556
+ Logger . Info ( $ "Downloading { fileRelativePath } from \n -{ url } \n ") ;
555
557
// get relative path correct
556
558
saveAsDir = Path . GetDirectoryName ( Path . Combine ( saveAsDir , relativePath ) ) ;
557
559
saveAsDir = Path . GetFullPath ( saveAsDir ) ;
558
560
var path = await Helper . DownloadUrlAsync ( url . ToString ( ) , saveAsDir , reportProgressAction , null , cancelToken ) ;
559
561
560
- LogHelper . LogInfo ( $ "Saved { fileRelativePath } to { path } ") ;
562
+ Logger . Info ( $ "Saved { fileRelativePath } to { path } ") ;
561
563
return path ;
562
564
}
563
565
@@ -646,7 +648,7 @@ public static async Task<string> DownloadUrlAsync(string url, string saveAsDir,
646
648
647
649
Directory . CreateDirectory ( saveAsDir ) ;
648
650
var file = Path . Combine ( saveAsDir , fileName ) ;
649
- LogHelper . LogInfo ( $ "Downloading { url } ") ;
651
+ Logger . Info ( $ "Downloading { url } ") ;
650
652
using ( WebClient wc = new WebClient ( ) )
651
653
{
652
654
var prog = 0 ;
@@ -669,7 +671,7 @@ public static async Task<string> DownloadUrlAsync(string url, string saveAsDir,
669
671
await t ;
670
672
if ( t . IsFaulted && t . Exception != null )
671
673
throw t . Exception ;
672
- LogHelper . LogInfo ( $ "Saved { fileName } to { file } ") ;
674
+ Logger . Info ( $ "Saved { fileName } to { file } ") ;
673
675
}
674
676
catch ( WebException ex ) when ( ex . Status == WebExceptionStatus . RequestCanceled )
675
677
{
@@ -689,7 +691,7 @@ public static async Task<string> DownloadUrlAsync(string url, string saveAsDir,
689
691
690
692
if ( ! File . Exists ( file ) )
691
693
{
692
- throw LogHelper . LogReturnError ( $ "Failed to download { fileName } ") ;
694
+ throw Logger . ReturnError ( $ "Failed to download { fileName } ") ;
693
695
}
694
696
var outputDirOrFile = file ;
695
697
@@ -700,7 +702,7 @@ public static async Task<string> DownloadUrlAsync(string url, string saveAsDir,
700
702
}
701
703
catch ( Exception e )
702
704
{
703
- throw LogHelper . LogReturnError ( e , $ "Unable to unzip file { Path . GetFileName ( file ) } ") ;
705
+ throw Logger . ReturnError ( $ "Unable to unzip file { Path . GetFileName ( file ) } \n { e } ") ;
704
706
}
705
707
706
708
return outputDirOrFile ;
0 commit comments