Skip to content

Instantly share code, notes, and snippets.

View RhysC's full-sized avatar

Rhys Campbell RhysC

  • Perth; Australia
View GitHub Profile
@RhysC
RhysC / Azure_GetLastDaysLogsFromStorage.cs
Last active December 31, 2015 02:58
Gets all the blobs in wad-log4net from the last 24 hours (by last mod date). Also can be found here for easy linq pad use : http://share.linqpad.net/hltnqf.linq
void Main()
{
var accounts = new Dictionary<string,string>{
{"accountname", "accountkey"},
};
var latestLogsByAppName = GetLatestLogsByAppName(accounts);
latestLogsByAppName//.Where (l => !l.ApplicationName.Contains("WebRole") )
//.Where(l => l.LastModified < DateTime.UtcNow.AddDays(-1))
.OrderBy (l => l.LastModified)
@RhysC
RhysC / PartialGlobal.asax
Last active December 31, 2015 19:59
Scratch working of registering Mediator QueryHandlers with Autofac using the Query type interface, not their actual type for ShortBus mediator pattern (https://github.com/mhinze/ShortBus). Why? Because the Query implementations could/should be view models that are Web specific however the contract doesn't care about any of the crap that may be o…
private static void ConfigureContainer()
{
var containerBuilder = new ContainerBuilder();
var assembly = Assembly.GetExecutingAssembly();
containerBuilder.RegisterControllers(assembly);
containerBuilder.RegisterType<Mediator>().As<IMediator>();
containerBuilder.RegisterAssemblyTypes(typeof(CustomerService).Assembly)
.AsImplementedInterfaces();//Will regiter the
@RhysC
RhysC / 1_MVCHelper.txt
Last active January 2, 2016 16:28
MVC test helpers (.net 4.5) using Log4net, NSubstitute and xUnit. Intend for : - checking attributation of the controller and actions. - asserting ActionResult values in a fluent manner - reducing set up noise for underling type that help form a testable controller
Example usages:
public class MyControllerFixture : IUseFixture<ControllerFixtureInit>
{
private readonly MyController _sut;
private ControllerTestContext _controllerTestContext;
private readonly IMyDependency _dependency;
public MyControllerFixture()
{
@RhysC
RhysC / Get-AzureEmulatorErrors.ps1
Created January 13, 2014 02:14
get the warn and errors for the azure emulator log file (assuming pretty std log4net set up)
function Get-AzureEmulatorErrors
{
$LocalApplicationDataPath = [Environment]::GetFolderPath([System.Environment+SpecialFolder]::LocalApplicationData)
$AzureResourceRoot = "$LocalApplicationDataPath\dftmp\Resources"
Write-Host "Local Azure Resource Root: $AzureResourceRoot"
pushd "$AzureResourceRoot"
$logs = (dir -Recurse *\directory\LoggingStorage\*.log)
$ErrorAndWarnLines = $logs | Select-String -pattern "\[ERROR\]|\[WARN \]"
$entries = $ErrorAndWarnLines | select LineNumber, `
Line, `
@RhysC
RhysC / gist:11362100
Created April 28, 2014 04:54
asafaweb Excessive headers fixes
//On start up :
MvcHandler.DisableMvcResponseHeader = true;
// as an http module/global asax
protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
// Remove the "Server" HTTP Header from response - security review recommends against sending this
var app = sender as HttpApplication;
if (app == null || null == app.Context) return;
@RhysC
RhysC / UnhandledException.cs
Created May 12, 2014 06:35
Events that you should be using to get UnhandledExceptions
// .NET/CLR app domains
// You - always want to be logging these errors!
AppDomain.CurrentDomain.UnhandledException += (s,e) => {};
// Framework specific handlers
System.Windows.Application.Current.DispatcherUnhandledException += (s,e) => {}; // WPF
System.Windows.Forms.Application.ThreadException += (s,e) => {}; // Windows Forms
System.Threading.Tasks.TaskScheduler.UnobservedTaskException += (s,e) => {}; // TPL - not really very imporant
// ASP.NET (i'm guessing you hook into this in your Global.asx?)
@RhysC
RhysC / ExpectedObjectJasmineMatcher.js
Last active August 29, 2015 14:03
Expected Object like approach in Jasmine - Tags - JavaScript JS TDD Spec ExpectedObject Matcher addMatchers 2.0
var customMatchers = {
toInclude: function (util, customEqualityTesters) {
return {
compare: function (actual, expected) {
if (expected === undefined) {
expected = '';
}
var result = {
message: '',
@RhysC
RhysC / CmsController.cs
Last active August 29, 2015 14:04
Looking at a prisimic backed semi structure content delivery system...(TM) ;)
public class CmsController : BootstrapBaseController
{
private readonly PrismicProxy _prismicProxy;
public CmsController(PrismicProxy prismicProxy)
{
_prismicProxy = prismicProxy;
}
public ActionResult Dynamic(string id)
@RhysC
RhysC / CacheTests.cs
Last active August 29, 2015 14:05
Cache of T
using System;
using System.Threading;
using Xunit;
namespace MyNameSpace.Tests
{
public class CacheTests
{
[Fact]
public void CanReuseCachedObject()
@RhysC
RhysC / RequireApiKey
Created August 28, 2014 08:18
RequireApiKey require an api key for MVC controllers assumes SSL
public class RequireApiKey : ActionFilterAttribute
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(RequireApiKey));
public override void OnActionExecuting(HttpActionContext context)
{
var ipAddress = GetIpAddress(context);
Logger.InfoFormat("API attempt. Uri {0} - IP {1} - Headers {2} ", context.Request.RequestUri, ipAddress, context.Request.Headers);
IEnumerable<string> values;
if (context.Request.Headers.TryGetValues("ApiKey", out values) && GetApiKeys().Any (x => x ==values.First())