44using Microsoft . Extensions . Hosting ;
55using Microsoft . Extensions . Logging ;
66using CoreHelpers . Extensions . Logging . Tasks ;
7+ using CoreHelpers . TaskLogging . Sample ;
78
89// configure the services that they us the emulator
910HostApplicationBuilder builder = Host . CreateApplicationBuilder ( args ) ;
1718 . AddConsole ( )
1819 . AddTaskLogger ( ) ) ;
1920
21+ builder . Services . AddTransient < IProcessor , ProcessorSuccess > ( ) ;
22+ builder . Services . AddTransient < IProcessor , ProcessorFailed > ( ) ;
23+ builder . Services . AddTransient < Worker > ( ) ;
24+
2025// build the host
2126using IHost host = builder . Build ( ) ;
2227
23- // get the task logger factory
24- var taskLoggerFactory = host . Services . GetService < ITaskLoggerFactory > ( ) ;
25- if ( taskLoggerFactory == null )
26- throw new NullReferenceException ( ) ;
27-
28-
29- // define a bit meta data
30- var metaData = new Dictionary < string , string > ( )
31- {
32- { "app" , "CoreHelpers.TaskLogging.Sample" } ,
33- { "class" , "Main" }
34- } ;
35-
36- // Announce a new task in the system
37- var taskId = await taskLoggerFactory . AnnounceTask ( "SyncMyMails" , "q:Queue01" , "Container01" , metaData ) ;
38-
39- // ... this is the place where the application can do other topics, be aware
40- // the task is still announced and exists as pending task ...
41-
42- // get a logger factory
43- var loggerFactory = host . Services . GetService < ILoggerFactory > ( ) ;
44- if ( loggerFactory == null )
28+ // get the worker
29+ var worker = host . Services . GetService < Worker > ( ) ;
30+ if ( worker == null )
4531 throw new NullReferenceException ( ) ;
4632
47- // get a logger
48- var logger = loggerFactory . CreateLogger ( "Main" ) ;
49-
50- // generate scope
51- using ( logger . BeginScope ( "This is just a scope" ) )
52- {
53- using ( logger . BeginScope ( "This is just another scope" ) )
54- {
55- // after this using all what is logged becomes part of the
56- // task context
57- using ( logger . BeginTaskScope ( taskId ) )
58- {
59- // log some lines
60- for ( int i = 0 ; i < 500 ; i ++ )
61- logger . LogInformation ( $ "{ i } Hello World Task 1") ;
62- }
63- }
64-
65- // announce the second task
66- var taskId2 = await taskLoggerFactory . AnnounceTask ( "SomethingElseWithErrors" , "q:Queue01" , "Container01" ) ;
67-
68- // this would be a second task
69- using ( logger . BeginTaskScope ( taskId2 ) )
70- {
71- try
72- {
73- // log some lines
74- for ( int i = 0 ; i < 10 ; i ++ )
75- logger . LogInformation ( $ "{ i } Hello World Task 2") ;
76-
77- throw new Exception ( "I failed!" ) ;
78- } catch ( Exception e )
79- {
80- logger . LogError ( e , "Unknown Error" ) ;
81- }
82- }
83- }
84-
85- // generate a new task the system never announced before
86- using ( logger . BeginNewTaskScope ( "NewTask" , "Q:Q2" , "Cnt02" ) )
87- {
88- logger . LogInformation ( "hello new task" ) ;
89- }
33+ // execute the work
34+ await worker . Process ( ) ;
0 commit comments