Skip to content
Permalink
Browse files

Added in a demonstration of the use of ContextProperties, with Destru…

…cturing example.
  • Loading branch information...
Adam Grande
Adam Grande committed Nov 1, 2016
1 parent 5d7b765 commit 7e2a5a06cbc7913b762631b2a00eb493db4328c8
@@ -11,6 +11,7 @@
using Autofac.Integration.Mvc;
using AutofacSerilogIntegration;
using Serilog;
using SerilogPoc.Data;
using SerilogWeb.Classic.Enrichers;

namespace SerilogPoc
@@ -26,6 +27,7 @@ public static void Configure()
ConfigureSerilog(builder);

builder.RegisterControllers(typeof(WebApiApplication).Assembly);
builder.RegisterType<PlayerService>().AsImplementedInterfaces();

Container = builder.Build();

@@ -1,22 +1,48 @@
using System.Web.Mvc;
using Serilog;
using Serilog.Context;
using SerilogPoc.Data;

namespace SerilogPoc.Controllers
{
public class HomeController : Controller
{
private readonly ILogger _logger;
private readonly IPlayerService _playerService;

public HomeController(ILogger logger)
public HomeController(ILogger logger, IPlayerService playerService)
{
_logger = logger;
_playerService = playerService;
}
public ActionResult Index()
{
_logger.Information("Invoking Index() Action with injected logger");
_logger.Information("Attempting to load all players.");
ViewBag.Title = "Home Page";
_logger.Information("Finished Index() Action with injected logger");
return View();

var players = _playerService.GetPlayers();

if(players.Count > 0)
_logger.Information("Found players");

return View(players);
}

public ActionResult Player(string alias)
{
_logger.Information("Attempting to find player {alias}.", alias);
var player = _playerService.GetPlayer(alias);
if (player == null)
{
using (LogContext.PushProperty("PlayerCount", _playerService.GetPlayers().Count))
_logger.Warning("Player {alias} not found. Valid players include: {players}", alias, _playerService.GetPlayers());

_logger.Information("Redirecting to Index Action");
return RedirectToAction("Index");
}

_logger.Information("Player with alias {alias} found, with name {name}", alias, player.Name);
return View(player);
}
}
}
@@ -0,0 +1,49 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using SerilogPoc.Models;


namespace SerilogPoc.Data
{
public class PlayerService : IPlayerService
{
private List<Player> _defaultStore;

public PlayerService()
{
_defaultStore = new List<Player>();
LoadData();
}
public List<Player> GetPlayers()
{
return _defaultStore;

}

public Player GetPlayer(string alias)
{
return _defaultStore.FirstOrDefault(f => f.Alias == alias);
}

private void LoadData()
{
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("SerilogPoc.Data.players.json"))
{
using (var reader = new StreamReader(stream))
{
var result = reader.ReadToEnd();
_defaultStore = JsonConvert.DeserializeObject<List<Player>>(result);
}
}
}
}

public interface IPlayerService
{
List<Player> GetPlayers();
Player GetPlayer(string alias);
}
}
@@ -0,0 +1,51 @@
[
{
"Alias": "GeT_RiGhT",
"Name": "Christopher Alesund",
"Team": "Ninjas in Pyjamas",
"Country": "Sweden",
"Url": "http://wiki.teamliquid.net/counterstrike/GeT_RiGhT"
},
{
"Alias": "S1mple",
"Name": "Oleksandr Kostyliev",
"Team": "Natus Vincere",
"Country": "Ukraine",
"Url": "http://wiki.teamliquid.net/counterstrike/S1mple"
},
{
"Alias": "Hiko",
"Name": "Spencer Martin",
"Team": "Team Liquid",
"Country": "America",
"Url": "http://wiki.teamliquid.net/counterstrike/Hiko"
},
{
"Alias": "Yam",
"Name": "Yaman Ergenekon",
"Team": "Renegades",
"Country": "Australia",
"Url": "http://wiki.teamliquid.net/counterstrike/Yam"
},
{
"Alias": "N0thing",
"Name": "Jordan Gilbert",
"Team": "Cloud9",
"Country": "America",
"Url": "http://wiki.teamliquid.net/counterstrike/N0thing"
},
{
"Alias": "Cajunb",
"Name": "René Borg",
"Team": "Team Dignitas",
"Country": "Denmark",
"Url": "http://wiki.teamliquid.net/counterstrike/cajunb"
},
{
"Alias": "Dupreeh",
"Name": "Peter Rothmann",
"Team": "Astralis",
"Country": "Denmark",
"Url": "http://wiki.teamliquid.net/counterstrike/dupreeh"
}
]
@@ -0,0 +1,16 @@
namespace SerilogPoc.Models
{
public class Player
{
public string Alias { get; set; }
public string Name { get; set; }
public string Team { get; set; }
public string Country { get; set; }
public string Url { get; set; }

public override string ToString()
{
return Alias;
}
}
}
@@ -188,19 +188,19 @@
<HintPath>..\packages\Antlr.3.4.1.9004\lib\Antlr3.Runtime.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Folder Include="Models\" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Compile Include="App_Start\BundleConfig.cs" />
<Compile Include="App_Start\FilterConfig.cs" />
<Compile Include="App_Start\IoCConfig.cs" />
<Compile Include="App_Start\RouteConfig.cs" />
<Compile Include="App_Start\WebApiConfig.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Data\PlayerService.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Models\Player.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
@@ -213,6 +213,7 @@
<Content Include="Scripts\ai.0.22.19-build00125.min.js" />
<Content Include="Scripts\bootstrap.js" />
<Content Include="Scripts\bootstrap.min.js" />
<EmbeddedResource Include="Data\players.json" />
<None Include="Scripts\jquery-1.10.2.intellisense.js" />
<Content Include="Scripts\jquery-1.10.2.js" />
<Content Include="Scripts\jquery-1.10.2.min.js" />
@@ -233,6 +234,7 @@
<Content Include="Views\Home\Index.cshtml" />
<Content Include="Views\Shared\Error.cshtml" />
<Content Include="Views\Shared\_Layout.cshtml" />
<Content Include="Views\Home\Player.cshtml" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
@@ -1,24 +1,14 @@
<div class="jumbotron">
<h1>ASP.NET</h1>
<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS, and JavaScript.</p>
<p><a href="http://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
</div>
<div class="row">
<div class="col-md-4">
<h2>Getting started</h2>
<p>ASP.NET Web API is a framework that makes it easy to build HTTP services that reach
a broad range of clients, including browsers and mobile devices. ASP.NET Web API
is an ideal platform for building RESTful applications on the .NET Framework.</p>
<p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301870">Learn more &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>Get more libraries</h2>
<p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
<p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301871">Learn more &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>Web Hosting</h2>
<p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
<p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301872">Learn more &raquo;</a></p>
</div>
</div>
@model List<SerilogPoc.Models.Player>

<div class="container">
<br/>
@foreach (var f in Model)
{
<div class="row">
<div class="col-md-3">@f.Name :</div>
<div class="col-md-5"><a href="@Url.Action("Player", new {alias = f.Alias})">@f.Alias</a>
</div>
</div>
}

</div>
@@ -0,0 +1,39 @@
@model SerilogPoc.Models.Player

<div class="container">
<div class="row">
<div class="col-md-8"><h1>@Model.Name</h1></div>
</div>
<div class="row">
<div class="col-md-3">
Alias:
</div>
<div class="col-md-5">
@Model.Alias
</div>
</div>
<div class="row">
<div class="col-md-3">
Country:
</div>
<div class="col-md-5">
@Model.Country
</div>
</div>
<div class="row">
<div class="col-md-3">
Team:
</div>
<div class="col-md-5">
@Model.Team
</div>
</div>
<div class="row">
<div class="col-md-3">
More Information:
</div>
<div class="col-md-5">
<a href="@Model.Url">@Model.Alias</a>
</div>
</div>
</div>
@@ -11,7 +11,7 @@
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

<!-- This is the default installation URL of the installation of Seq. Feel free to adjust to whatever site/port you want. -->
<add key="SeqUrl" value="http://iamabadurl:5341/" />
<add key="SeqUrl" value="http://localhost:5341/" />

</appSettings>
<system.web>

0 comments on commit 7e2a5a0

Please sign in to comment.
You can’t perform that action at this time.