11using System . Threading . Tasks ;
22using Microsoft . Azure . Functions . Worker ;
33using Microsoft . Azure . Functions . Worker . Http ;
4+ using Microsoft . Extensions . Logging ;
45using Microsoft . OneFuzz . Service . Auth ;
56using Microsoft . OneFuzz . Service . OneFuzzLib . Orm ;
6-
77namespace Microsoft . OneFuzz . Service . Functions ;
88
99public class AgentEvents {
10- private readonly ILogTracer _log ;
10+ private readonly ILogger _log ;
1111 private readonly IOnefuzzContext _context ;
1212
13- public AgentEvents ( ILogTracer log , IOnefuzzContext context ) {
13+ public AgentEvents ( ILogger < AgentEvents > log , IOnefuzzContext context ) {
1414 _log = log ;
1515 _context = context ;
1616 }
@@ -26,7 +26,8 @@ public async Async.Task<HttpResponseData> Run(
2626 }
2727
2828 var envelope = request . OkV ;
29- _log . WithTag ( "HttpRequest" , "POST" ) . Info ( $ "node event: { envelope . MachineId : Tag:MachineId} { EntityConverter . ToJsonString ( envelope ) : Tag:Event} ") ;
29+ _log . AddTag ( "HttpRequest" , "POST" ) ;
30+ _log . LogInformation ( "node event: {MachineId} {Event}" , envelope . MachineId , EntityConverter . ToJsonString ( envelope ) ) ;
3031
3132 var error = envelope . Event switch {
3233 NodeStateUpdate updateEvent => await OnStateUpdate ( envelope . MachineId , updateEvent ) ,
@@ -63,7 +64,7 @@ public async Async.Task<HttpResponseData> Run(
6364 private async Async . Task < Error ? > OnStateUpdate ( Guid machineId , NodeStateUpdate ev ) {
6465 var node = await _context . NodeOperations . GetByMachineId ( machineId ) ;
6566 if ( node is null ) {
66- _log . Warning ( $ "unable to process state update event. { machineId : Tag: MachineId} { ev : Tag: Event} ") ;
67+ _log . LogWarning ( "unable to process state update event. {MachineId} {Event}" , machineId , ev ) ;
6768 return null ;
6869 }
6970
@@ -72,14 +73,14 @@ public async Async.Task<HttpResponseData> Run(
7273 if ( ! node . Managed ) {
7374 return null ;
7475 }
75- _log . Info ( $ "stopping free node with reset flags: { machineId : Tag: MachineId} ") ;
76+ _log . LogInformation ( "stopping free node with reset flags: {MachineId}" , machineId ) ;
7677 // discard result: node not used after this point
7778 _ = await _context . NodeOperations . Stop ( node ) ;
7879 return null ;
7980 }
8081
8182 if ( await _context . NodeOperations . CouldShrinkScaleset ( node ) ) {
82- _log . Info ( $ "stopping free node to resize scaleset: { machineId : Tag: MachineId} ") ;
83+ _log . LogInformation ( "stopping free node to resize scaleset: {MachineId}" , machineId ) ;
8384 // discard result: node not used after this point
8485 _ = await _context . NodeOperations . SetHalt ( node ) ;
8586 return null ;
@@ -88,7 +89,7 @@ public async Async.Task<HttpResponseData> Run(
8889
8990 if ( ev . State == NodeState . Init ) {
9091 if ( node . DeleteRequested ) {
91- _log . Info ( $ "stopping node (init and delete_requested): { machineId : Tag: MachineId} ") ;
92+ _log . LogInformation ( "stopping node (init and delete_requested): {MachineId}" , machineId ) ;
9293 // discard result: node not used after this point
9394 _ = await _context . NodeOperations . Stop ( node ) ;
9495 return null ;
@@ -106,7 +107,7 @@ public async Async.Task<HttpResponseData> Run(
106107 node = await _context . NodeOperations . SetState ( node , ev . State ) ;
107108
108109 if ( ev . State == NodeState . Free ) {
109- _log . Info ( $ "node now available for work: { machineId : Tag: MachineId} ") ;
110+ _log . LogInformation ( "node now available for work: {MachineId}" , machineId ) ;
110111 } else if ( ev . State == NodeState . SettingUp ) {
111112 if ( ev . Data is NodeSettingUpEventData settingUpData ) {
112113 if ( ! settingUpData . Tasks . Any ( ) ) {
@@ -123,7 +124,7 @@ public async Async.Task<HttpResponseData> Run(
123124 $ "unable to find task: { taskId } ") ;
124125 }
125126
126- _log . Info ( $ "node starting task. { machineId : Tag: MachineId} { task . JobId : Tag:JobId } { task . TaskId : Tag:TaskId } " ) ;
127+ _log . LogInformation ( "node starting task. {MachineId} {JobId} {TaskId}" , machineId , task . JobId , task . TaskId ) ;
127128
128129 // The task state may be `running` if it has `vm_count` > 1, and
129130 // another node is concurrently executing the task. If so, leave
@@ -141,7 +142,8 @@ public async Async.Task<HttpResponseData> Run(
141142 State : NodeTaskState . SettingUp ) ;
142143 var r = await _context . NodeTasksOperations . Replace ( nodeTask ) ;
143144 if ( ! r . IsOk ) {
144- _log . WithHttpStatus ( r . ErrorV ) . Error ( $ "Failed to replace node task { task . TaskId : Tag:TaskId} ") ;
145+ _log . AddHttpStatus ( r . ErrorV ) ;
146+ _log . LogError ( "Failed to replace node task {TaskId}" , task . TaskId ) ;
145147 }
146148 }
147149 }
@@ -151,7 +153,7 @@ public async Async.Task<HttpResponseData> Run(
151153 if ( doneData . Error is not null ) {
152154 var errorText = EntityConverter . ToJsonString ( doneData ) ;
153155 error = Error . Create ( ErrorCode . TASK_FAILED , errorText ) ;
154- _log . Error ( $ "node 'done' { machineId : Tag: MachineId} - { errorText : Tag: Error} ") ;
156+ _log . LogError ( "node 'done' {MachineId} - {Error}" , machineId , errorText ) ;
155157 }
156158 }
157159
@@ -203,15 +205,16 @@ public async Async.Task<HttpResponseData> Run(
203205 State : NodeTaskState . Running ) ;
204206 var r = await _context . NodeTasksOperations . Replace ( nodeTask ) ;
205207 if ( ! r . IsOk ) {
206- _log . WithHttpStatus ( r . ErrorV ) . Error ( $ "failed to replace node task { nodeTask . TaskId : Tag:TaskId} ") ;
208+ _log . AddHttpStatus ( r . ErrorV ) ;
209+ _log . LogError ( "failed to replace node task {TaskId}" , nodeTask . TaskId ) ;
207210 }
208211
209212 if ( task . State . ShuttingDown ( ) ) {
210- _log . Info ( $ "ignoring task start from node. { machineId : Tag: MachineId} { task . JobId : Tag:JobId } { task . TaskId : Tag:TaskId } ({ task . State : Tag:State } )") ;
213+ _log . LogInformation ( "ignoring task start from node. {MachineId} {JobId} {TaskId} ({State})" , machineId , task . JobId , task . TaskId , task . State ) ;
211214 return null ;
212215 }
213216
214- _log . Info ( $ "task started on node. { machineId : Tag: MachineId} { task . JobId : Tag:JobId } { task . TaskId : Tag:TaskId } " ) ;
217+ _log . LogInformation ( "task started on node. {MachineId} {JobId} {TaskId}" , machineId , task . JobId , task . TaskId ) ;
215218 task = await _context . TaskOperations . SetState ( task , TaskState . Running ) ;
216219
217220 var taskEvent = new TaskEvent (
@@ -220,7 +223,8 @@ public async Async.Task<HttpResponseData> Run(
220223 EventData : new WorkerEvent ( Running : running ) ) ;
221224 r = await _context . TaskEventOperations . Replace ( taskEvent ) ;
222225 if ( ! r . IsOk ) {
223- _log . WithHttpStatus ( r . ErrorV ) . Error ( $ "failed to replace taskEvent { taskEvent . TaskId : Tag:TaskId} ") ;
226+ _log . AddHttpStatus ( r . ErrorV ) ;
227+ _log . LogError ( "failed to replace taskEvent {TaskId}" , taskEvent . TaskId ) ;
224228 }
225229
226230 return null ;
@@ -246,15 +250,16 @@ public async Async.Task<HttpResponseData> Run(
246250 } ;
247251
248252 if ( done . ExitStatus . Success ) {
249- _log . Info ( $ "task done. { task . JobId : Tag:JobId } :{ task . TaskId : Tag:TaskId } { done . ExitStatus : Tag: Status} ") ;
253+ _log . LogInformation ( "task done. {JobId}:{TaskId} {Status}" , task . JobId , task . TaskId , done . ExitStatus ) ;
250254 await _context . TaskOperations . MarkStopping ( task , "task is done" ) ;
251255
252256 // keep node if keep-on-completion is set
253257 if ( task . Config . Debug ? . Contains ( TaskDebugFlag . KeepNodeOnCompletion ) == true ) {
254258 node = node with { DebugKeepNode = true } ;
255259 var r = await _context . NodeOperations . Replace ( node ) ;
256260 if ( ! r . IsOk ) {
257- _log . WithHttpStatus ( r . ErrorV ) . Error ( $ "keepNodeOnCompletion: failed to replace node { node . MachineId : Tag:MachineId} when setting debug keep node to true") ;
261+ _log . AddHttpStatus ( r . ErrorV ) ;
262+ _log . LogError ( "keepNodeOnCompletion: failed to replace node {MachineId} when setting debug keep node to true" , node . MachineId ) ;
258263 }
259264 }
260265 } else {
@@ -273,22 +278,25 @@ await _context.TaskOperations.MarkFailed(
273278 node = node with { DebugKeepNode = true } ;
274279 var r = await _context . NodeOperations . Replace ( node ) ;
275280 if ( ! r . IsOk ) {
276- _log . WithHttpStatus ( r . ErrorV ) . Error ( $ "keepNodeOnfFailure: failed to replace node { node . MachineId : Tag:MachineId} when setting debug keep node to true") ;
281+ _log . AddHttpStatus ( r . ErrorV ) ;
282+ _log . LogError ( "keepNodeOnfFailure: failed to replace node {MachineId} when setting debug keep node to true" , node . MachineId ) ;
277283 }
278284 }
279285 }
280286
281287 if ( ! node . DebugKeepNode ) {
282288 var r = await _context . NodeTasksOperations . Delete ( new NodeTasks ( machineId , done . TaskId ) ) ;
283289 if ( ! r . IsOk ) {
284- _log . WithHttpStatus ( r . ErrorV ) . Error ( $ "failed to deleting node task { done . TaskId : Tag:TaskId} for: { machineId : Tag:MachineId} since DebugKeepNode is false") ;
290+ _log . AddHttpStatus ( r . ErrorV ) ;
291+ _log . LogError ( "failed to deleting node task {TaskId} for: {MachineId} since DebugKeepNode is false" , done . TaskId , machineId ) ;
285292 }
286293 }
287294
288295 var taskEvent = new TaskEvent ( done . TaskId , machineId , new WorkerEvent { Done = done } ) ;
289296 var r1 = await _context . TaskEventOperations . Replace ( taskEvent ) ;
290297 if ( ! r1 . IsOk ) {
291- _log . WithHttpStatus ( r1 . ErrorV ) . Error ( $ "failed to update task event for done task { done . TaskId : Tag:TaskId} ") ;
298+ _log . AddHttpStatus ( r1 . ErrorV ) ;
299+ _log . LogError ( "failed to update task event for done task {TaskId}" , done . TaskId ) ;
292300 }
293301 return null ;
294302 }
0 commit comments