@@ -27,11 +27,11 @@ public class Sample
2727 public record struct HttpRequestDurationKey ( string Path , int StatusCode ) ;
2828 public readonly Dictionary < HttpRequestDurationKey , ExponentialHistogram > HttpRequestDuration = new ( ) ;
2929
30- // `OrdersCreated `: counter
31- public ulong OrdersCreated ;
30+ // `OrderCreated `: counter
31+ public ulong OrderCreated ;
3232
33- // `OrdersShipped `: counter
34- public ulong OrdersShipped ;
33+ // `OrderShipped `: counter
34+ public ulong OrderShipped ;
3535
3636 static readonly MessageTemplate Template = new MessageTemplateParser ( ) . Parse ( "Metrics sampled" ) ;
3737
@@ -45,20 +45,25 @@ public IEnumerable<LogEvent> ToLogEvents(ILogger logger, PropertyNameMapping pro
4545 timestamp ,
4646 new Dictionary < string , object >
4747 {
48- { "HttpRequestDuration" , new { kind = "Exponential" , unit = "ms" , description = "The time taken to fully process a request" } }
48+ [ nameof ( HttpRequestDuration ) ] = new
49+ {
50+ kind = "Exponential" ,
51+ unit = "ms" ,
52+ description = "The time taken to fully process a request."
53+ }
4954 } ,
50- new
55+ new Dictionary < string , object >
5156 {
52- HttpRequestDuration = new {
57+ [ nameof ( HttpRequestDuration ) ] = new {
5358 buckets = metric . Buckets
5459 . Select ( bucket => new { midpoint = bucket . Key , count = bucket . Value } ) . ToArray ( ) ,
5560 scale = metric . Scale ,
5661 min = metric . Min ,
5762 max = metric . Max ,
5863 count = metric . Total
5964 } ,
60- key . Path ,
61- key . StatusCode
65+ [ nameof ( key . Path ) ] = key . Path ,
66+ [ nameof ( key . StatusCode ) ] = key . StatusCode
6267 }
6368 ) ;
6469 }
@@ -69,24 +74,46 @@ public IEnumerable<LogEvent> ToLogEvents(ILogger logger, PropertyNameMapping pro
6974 timestamp ,
7075 new Dictionary < string , object >
7176 {
72- { "OrderCreated" , new { kind = "Sum" , unit = "orders" , description = "An order was created" } } ,
73- { "OrderShipped" , new { kind = "Sum" , unit = "orders" , description = "An order was shipped" } }
77+ [ nameof ( OrderCreated ) ] = new
78+ {
79+ kind = "Sum" ,
80+ unit = "order" ,
81+ description = "An order was created."
82+ } ,
83+ [ nameof ( OrderShipped ) ] = new
84+ {
85+ kind = "Sum" ,
86+ unit = "order" ,
87+ description = "An order was shipped."
88+ }
7489 } ,
75- new
90+ new Dictionary < string , object >
7691 {
77- OrdersCreated ,
78- OrdersShipped
92+ [ nameof ( OrderCreated ) ] = OrderCreated ,
93+ [ nameof ( OrderShipped ) ] = OrderShipped
7994 }
8095 ) ;
8196 }
8297
83- static LogEvent ToLogEvent ( ILogger logger , PropertyNameMapping propertyNameMapping , DateTimeOffset timestamp , Dictionary < string , object > definitions , object samples )
98+ static LogEvent ToLogEvent ( ILogger logger , PropertyNameMapping propertyNameMapping , DateTimeOffset timestamp , Dictionary < string , object > definitions , Dictionary < string , object > samples )
8499 {
85- logger . BindProperty ( propertyNameMapping . MetricDefinitions , definitions , true , out var definitionsProperty ) ;
86- logger . BindProperty ( propertyNameMapping . MetricSamples , samples , true , out var sampleProperty ) ;
100+ var properties = new List < LogEventProperty > ( ) ;
101+
102+ if ( logger . BindProperty ( propertyNameMapping . MetricDefinitions , definitions , true ,
103+ out var definitionsProperty ) )
104+ {
105+ properties . Add ( definitionsProperty ) ;
106+ }
107+
108+ foreach ( var ( key , value ) in samples )
109+ {
110+ if ( logger . BindProperty ( key , value , true , out var sample ) )
111+ {
112+ properties . Add ( sample ) ;
113+ }
114+ }
87115
88- return new LogEvent ( timestamp , LogEventLevel . Information , null , Template ,
89- [ definitionsProperty ! , sampleProperty ! ] ) ;
116+ return new LogEvent ( timestamp , LogEventLevel . Information , null , Template , properties ) ;
90117 }
91118 }
92119
@@ -113,15 +140,15 @@ public void RecordOrderCreated()
113140 {
114141 lock ( _lock )
115142 {
116- _current . OrdersCreated += 1 ;
143+ _current . OrderCreated += 1 ;
117144 }
118145 }
119146
120147 public void RecordOrderShipped ( )
121148 {
122149 lock ( _lock )
123150 {
124- _current . OrdersShipped += 1 ;
151+ _current . OrderShipped += 1 ;
125152 }
126153 }
127154
0 commit comments