Skip to content

Instantly share code, notes, and snippets.

@DTTerastar
DTTerastar / gist:7917094
Last active December 31, 2015 01:49
Log a function call easily w/ log4net. Shouldn't have performance impact if not set to log. Uses [CallerMemberName] so you don't have to supply it. Usage: logger.InfoCall(()=>new { param1, param2, param3})
public static class LogExtensions
{
public static void InfoCall(this ILog log, Func<object> args = null, [CallerMemberName] string name = "")
{
if (log.IsInfoEnabled)
log.Info(new Message(name, (args != null) ? args() : null));
}
public static void DebugCall(this ILog log, Func<object> args = null, [CallerMemberName] string name = "")
{
@DTTerastar
DTTerastar / gist:7327308
Created November 5, 2013 22:16
ResolveUrls without page reference. Makes rooted relative urls. Has very simple syntax with anonymous objects for query string params.
public static string GlobalResolveUrl(string url)
{
return (new Uri(HttpContext.Current.Request.Url, ResolveUrl(url))).GetLeftPart(UriPartial.Query);
}
public static string GlobalResolveUrl(string url, object queryStringParams)
{
return (new Uri(HttpContext.Current.Request.Url, ResolveUrl(url, queryStringParams))).GetLeftPart(UriPartial.Query);
}
@DTTerastar
DTTerastar / gist:6940423
Created October 11, 2013 19:14
Split and trim a string. Does not return empty strings.
public static IEnumerable<string> SplitTrim(this string input, params char[] separator)
{
if (input == null) throw new ArgumentNullException("input");
string[] strings = input.Trim().Split(separator);
foreach (var s in strings)
{
string trimmed = s.Trim();
if (!String.IsNullOrWhiteSpace(trimmed))
yield return trimmed;
}
@DTTerastar
DTTerastar / gist:6842546
Created October 5, 2013 15:51
Nlog to Papertrail
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<extensions>
<add assembly="NLog.Targets.Syslog" />
</extensions>
<targets async="true">
<target xsi:type="File" name="f" fileName="${basedir}/App_Data/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" />
<target name="papertrail" type="Syslog" syslogserver="logs.papertrailapp.com" port="*fillin*" facility="Local7" layout="${uppercase:${level}} ${message} ${exception}"/>
@DTTerastar
DTTerastar / Dumper.cs
Last active August 11, 2018 23:19
Nice easy way of getting formatted C# like serialized text from object graphs. Nice to use for creating unit tests, or checking on intermediary values while debugging.
public static class DumperExtensions
{
public static string DumpToString(this object a, int maxDepth = 16, bool breakCircularRefs = false)
{
return new Dumper(a, maxDepth, breakCircularRefs).ToString();
}
public static T DumpToConsole<T>(this T a, int maxDepth = 16, bool breakCircularRefs = false)
{
Console.Out.WriteLine(DumpToString(a, maxDepth, breakCircularRefs));
@DTTerastar
DTTerastar / RouteCollectionExtensions.cs
Last active December 22, 2015 16:19
MapRedirect for temp and perm redirecting
public static class RouteCollectionExtensions
{
public static void MapRedirect(this RouteCollection rc, string url, string dest, bool perm = false)
{
rc.Add(new Route(url, new RedirectRouteHandler(dest, perm)));
}
}
public class VirtualPathRouteHandlerBase
{
@DTTerastar
DTTerastar / Asterisk.cs
Created February 14, 2013 15:30
Validate by using IValidatableObject, or by DbEntityValidationException. The asterisk control with the correct member name will show which fields need updating, while all error messages will show in ValidationSummary.
public static class AsteriskValidation
{
public static void Validate(this Page page, IValidatableObject validatableObject, ValidationContext context = null)
{
foreach (ValidationResult error in validatableObject.Validate(context))
page.Validators.Add(new ErrorValidator(error));
}
public static void Validate(this Page page, DbEntityValidationException ex, ValidationContext context = null)
{
public class NavHistory : Control
{
private readonly List<Tuple<string, Action<string>>> _setters = new List<Tuple<string, Action<string>>>();
private Dictionary<string, string> _changes = new Dictionary<string, string>();
private ScriptManager _sm;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Page.Init += Page_Init;
public static TValue GetOrSet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> create)
{
TValue value;
return dictionary.TryGetValue(key, out value) ? value : (dictionary[key] = create(key));
}
public static TValue GetOrSet<TKey, TValue>(this IDictionary dictionary, TKey key, Func<TKey, TValue> create)
{
var value = (TValue) dictionary[key];
if (!Equals(value, default(TValue))) return value;
; Variables definition
; -----------------------------------------------------------------------------
EnvGet, userProfile, USERPROFILE
Software := userProfile . "\Dropbox\software\"
; Launch or toggle program, http://lifehacker.com/5468862/create-a-shortcut-key-for-restoring-a-specific-window
; -----------------------------------------------------------------------------
ToggleWinMinimize(WindowTitle)
{
SetTitleMatchMode,2