Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Logging.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "SampleApp", "samples\Sample
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.Logging.Console", "src\Microsoft.Framework.Logging.Console\Microsoft.Framework.Logging.Console.kproj", "{75A4DE6D-BBAA-4D59-829D-94009E759A18}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Logging.Elm", "src\Microsoft.AspNet.Logging.Elm\Microsoft.AspNet.Logging.Elm.kproj", "{889A6055-2498-4AD0-A866-3B1638257D8B}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ElmSampleApp", "samples\ElmSampleApp\ElmSampleApp.kproj", "{55FB3329-DC96-461C-8E80-B409DF4B494C}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "PageGenerator", "src\PageGenerator\PageGenerator.kproj", "{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -89,12 +95,43 @@ Global
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.Release|x86.ActiveCfg = Release|Any CPU
{889A6055-2498-4AD0-A866-3B1638257D8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{889A6055-2498-4AD0-A866-3B1638257D8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{889A6055-2498-4AD0-A866-3B1638257D8B}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{889A6055-2498-4AD0-A866-3B1638257D8B}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{889A6055-2498-4AD0-A866-3B1638257D8B}.Debug|x86.ActiveCfg = Debug|Any CPU
{889A6055-2498-4AD0-A866-3B1638257D8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{889A6055-2498-4AD0-A866-3B1638257D8B}.Release|Any CPU.Build.0 = Release|Any CPU
{889A6055-2498-4AD0-A866-3B1638257D8B}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{889A6055-2498-4AD0-A866-3B1638257D8B}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{889A6055-2498-4AD0-A866-3B1638257D8B}.Release|x86.ActiveCfg = Release|Any CPU
{55FB3329-DC96-461C-8E80-B409DF4B494C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{55FB3329-DC96-461C-8E80-B409DF4B494C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55FB3329-DC96-461C-8E80-B409DF4B494C}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{55FB3329-DC96-461C-8E80-B409DF4B494C}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{55FB3329-DC96-461C-8E80-B409DF4B494C}.Debug|x86.ActiveCfg = Debug|Any CPU
{55FB3329-DC96-461C-8E80-B409DF4B494C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55FB3329-DC96-461C-8E80-B409DF4B494C}.Release|Any CPU.Build.0 = Release|Any CPU
{55FB3329-DC96-461C-8E80-B409DF4B494C}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{55FB3329-DC96-461C-8E80-B409DF4B494C}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{55FB3329-DC96-461C-8E80-B409DF4B494C}.Release|x86.ActiveCfg = Release|Any CPU
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Debug|x86.ActiveCfg = Debug|Any CPU
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Release|Any CPU.Build.0 = Release|Any CPU
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{4D4A785A-ECB9-4916-A88F-0FD306EE3B74}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{96B1D6A8-7E40-43C7-813F-898DC8192DDE} = {09920C51-6220-4D8D-94DC-E70C13446187}
{550E0247-0BDD-4016-A29B-250F075686FD} = {8C1F5D80-88EA-4961-84DC-7AC6E13951F4}
{55FB3329-DC96-461C-8E80-B409DF4B494C} = {8C1F5D80-88EA-4961-84DC-7AC6E13951F4}
EndGlobalSection
EndGlobal
18 changes: 18 additions & 0 deletions samples/ElmSampleApp/ElmSampleApp.kproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>55fb3329-dc96-461c-8e80-b409df4b494c</ProjectGuid>
<OutputType>Web</OutputType>
<RootNamespace>WebApplication2</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
<DevelopmentServerPort>42601</DevelopmentServerPort>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
43 changes: 43 additions & 0 deletions samples/ElmSampleApp/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Logging.Elm;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;

namespace ElmSampleApp
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IElmStore, ElmStore>(); // registering the service so it can be injected into constructors
services.Configure<ElmOptions>(options =>
{
options.Path = new PathString("/foo");
});
services.AddMvc();
}

public void Configure(IApplicationBuilder app, ILoggerFactory factory)
{
app.UseErrorPage();
app.UseElm();
app.UseMvc();
#pragma warning disable CS1998
app.Run(async context =>
#pragma warning restore CS1998
{
throw new InvalidOperationException();
});
}
}

public class HomeController
{
public string Index()
{
return "Hello World";
}
}
}
16 changes: 16 additions & 0 deletions samples/ElmSampleApp/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"webroot": "wwwroot",
"exclude": "wwwroot/**/*.*",
"dependencies": {
"Microsoft.AspNet.Diagnostics": "1.0.0-*",
"Microsoft.AspNet.Http": "1.0.0-*",
"Microsoft.AspNet.Logging.Elm": "1.0.0-*",
"Microsoft.AspNet.Mvc": "6.0.0-*",
"Microsoft.AspNet.PipelineCore": "1.0.0-*",
"Microsoft.AspNet.Server.IIS": "1.0.0-*"
},
"frameworks" : {
"aspnet50" : { },
"aspnetcore50" : { }
}
}
15 changes: 15 additions & 0 deletions src/Microsoft.AspNet.Logging.Elm/ElmExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNet.Logging.Elm;

namespace Microsoft.AspNet.Builder
{
public static class ElmExtensions
{
public static IApplicationBuilder UseElm(this IApplicationBuilder builder)
{
return builder.UseMiddleware<ElmMiddleware>();
}
}
}
107 changes: 107 additions & 0 deletions src/Microsoft.AspNet.Logging.Elm/ElmLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.AspNet.Http;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Elm;

namespace Microsoft.AspNet.Logging.Elm
{
public class ElmLogger : ILogger
{
private readonly string _name;
private readonly ElmLoggerProvider _provider;
private IElmStore _store;
private readonly IContextAccessor<HttpContext> _contextAccessor;
private readonly object _requestIdentifierKey;
private readonly object _logContextKey;

public ElmLogger(string name, ElmLoggerProvider provider, IElmStore store,
IContextAccessor<HttpContext> contextAccessor,
object requestIdentifierKey, object logContextKey)
{
_name = name;
_provider = provider;
_store = store;
_contextAccessor = contextAccessor;
_requestIdentifierKey = requestIdentifierKey;
_logContextKey = logContextKey;
}

public void Write(TraceType traceType, int eventId, object state, Exception exception,
Func<object, Exception, string> formatter)
{
LogInfo info = new LogInfo()
{
Context = GetLogContext(),
Name = _name,
EventID = eventId,
Severity = traceType,
Exception = exception,
State = state,
Time = DateTime.Now
};
if (ElmScope.Counts.ContainsKey(GetLogContext().RequestID))
{
// TODO: display nested scopes nicely
for (var i = 0; i < ElmScope.Counts[GetLogContext().RequestID].Count; i++)
{
state = "-----" + state;
}
info.State = state;
info.Scopes = new List<Guid>(ElmScope.Counts[GetLogContext().RequestID]);
}
_store.Add(info);
}

public bool IsEnabled(TraceType traceType)
{
return true;
}

public IDisposable BeginScope(object state)
{
return new ElmScope(this, state, GetLogContext().RequestID);
}

private LogContext GetLogContext()
{
var context = _contextAccessor.Value;
if (context == null)
{
// TODO: group non-request logs by Thread ID
return new LogContext()
{
ThreadID = Thread.CurrentThread.ManagedThreadId
};
}

var logContext = context.Items[_logContextKey] as LogContext;
if (logContext == null)
{
logContext = new LogContext()
{
RequestID = (Guid)context.Items[_requestIdentifierKey],
Host = context.Request.Host,
ContentType = context.Request.ContentType,
Path = context.Request.Path,
Scheme = context.Request.Scheme,
StatusCode = context.Response.StatusCode,
User = context.User,
Method = context.Request.Method,
Protocol = context.Request.Protocol,
Headers = context.Request.Headers,
Query = context.Request.QueryString,
Cookies = context.Request.Cookies
};
context.Items[_logContextKey] = logContext;
}

return logContext;
}
}
}
31 changes: 31 additions & 0 deletions src/Microsoft.AspNet.Logging.Elm/ElmLoggerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNet.Http;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;

namespace Microsoft.AspNet.Logging.Elm
{
public class ElmLoggerProvider : ILoggerProvider
{
private readonly IElmStore _store;
private readonly IContextAccessor<HttpContext> _contextAccessor;
private readonly object _requestIdentifierKey;
private readonly object _logContextKey;

public ElmLoggerProvider(IElmStore store, IContextAccessor<HttpContext> contextAccessor,
object requestIdentifierKey, object logContextKey)
{
_store = store;
_contextAccessor = contextAccessor;
_requestIdentifierKey = requestIdentifierKey;
_logContextKey = logContextKey;
}

public ILogger Create(string name)
{
return new ElmLogger(name, this, _store, _contextAccessor, _requestIdentifierKey, _logContextKey);
}
}
}
Loading