22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
44using System ;
5+ using System . Collections . Generic ;
6+ using System . Threading ;
57using Microsoft . AspNet . Http ;
68using Microsoft . Framework . DependencyInjection ;
79using Microsoft . Framework . Logging ;
10+ using Microsoft . Framework . Logging . Elm ;
811
912namespace Microsoft . AspNet . Logging . Elm
1013{
@@ -15,39 +18,23 @@ public class ElmLogger : ILogger
1518 private IElmStore _store ;
1619 private readonly IContextAccessor < HttpContext > _contextAccessor ;
1720 private readonly object _requestIdentifierKey ;
21+ private readonly object _logContextKey ;
1822
19- public ElmLogger ( string name , ElmLoggerProvider provider , IElmStore store , IContextAccessor < HttpContext > contextAccessor , object requestIdentifierKey )
23+ public ElmLogger ( string name , ElmLoggerProvider provider , IElmStore store ,
24+ IContextAccessor < HttpContext > contextAccessor ,
25+ object requestIdentifierKey , object logContextKey )
2026 {
2127 _name = name ;
2228 _provider = provider ;
2329 _store = store ;
2430 _contextAccessor = contextAccessor ;
2531 _requestIdentifierKey = requestIdentifierKey ;
32+ _logContextKey = logContextKey ;
2633 }
2734
28- public void Write ( TraceType traceType , int eventId , object state , Exception exception , Func < object , Exception , string > formatter )
35+ public void Write ( TraceType traceType , int eventId , object state , Exception exception ,
36+ Func < object , Exception , string > formatter )
2937 {
30- var message = string . Empty ;
31- if ( formatter != null )
32- {
33- message = formatter ( state , exception ) ;
34- }
35- else
36- {
37- if ( state != null )
38- {
39- message += state ;
40- }
41- if ( exception != null )
42- {
43- message += Environment . NewLine + exception ;
44- }
45- }
46- if ( string . IsNullOrEmpty ( message ) )
47- {
48- return ;
49- }
50-
5138 LogInfo info = new LogInfo ( )
5239 {
5340 Context = GetLogContext ( ) ,
@@ -58,7 +45,17 @@ public void Write(TraceType traceType, int eventId, object state, Exception exce
5845 State = state ,
5946 Time = DateTime . Now
6047 } ;
61- _store . Write ( info ) ;
48+ if ( ElmScope . Counts . ContainsKey ( GetLogContext ( ) . RequestID ) )
49+ {
50+ // TODO: display nested scopes nicely
51+ for ( var i = 0 ; i < ElmScope . Counts [ GetLogContext ( ) . RequestID ] . Count ; i ++ )
52+ {
53+ state = "-----" + state ;
54+ }
55+ info . State = state ;
56+ info . Scopes = new List < Guid > ( ElmScope . Counts [ GetLogContext ( ) . RequestID ] ) ;
57+ }
58+ _store . Add ( info ) ;
6259 }
6360
6461 public bool IsEnabled ( TraceType traceType )
@@ -68,30 +65,43 @@ public bool IsEnabled(TraceType traceType)
6865
6966 public IDisposable BeginScope ( object state )
7067 {
71- // TODO: use NullDisposable once it's moved to this repo #33
72- return null ;
68+ return new ElmScope ( this , state , GetLogContext ( ) . RequestID ) ;
7369 }
7470
7571 private LogContext GetLogContext ( )
7672 {
7773 var context = _contextAccessor . Value ;
7874 if ( context == null )
7975 {
80- return new LogContext ( ) ;
76+ // TODO: group non-request logs by Thread ID
77+ return new LogContext ( )
78+ {
79+ ThreadID = Thread . CurrentThread . ManagedThreadId
80+ } ;
8181 }
82- else
82+
83+ var logContext = context . Items [ _logContextKey ] as LogContext ;
84+ if ( logContext == null )
8385 {
84- return new LogContext ( )
86+ logContext = new LogContext ( )
8587 {
8688 RequestID = ( Guid ) context . Items [ _requestIdentifierKey ] ,
8789 Host = context . Request . Host ,
8890 ContentType = context . Request . ContentType ,
8991 Path = context . Request . Path ,
9092 Scheme = context . Request . Scheme ,
9193 StatusCode = context . Response . StatusCode ,
92- User = context . User . Identity . Name
94+ User = context . User ,
95+ Method = context . Request . Method ,
96+ Protocol = context . Request . Protocol ,
97+ Headers = context . Request . Headers ,
98+ Query = context . Request . QueryString ,
99+ Cookies = context . Request . Cookies
93100 } ;
101+ context . Items [ _logContextKey ] = logContext ;
94102 }
103+
104+ return logContext ;
95105 }
96106 }
97107}
0 commit comments