Skip to content

Instantly share code, notes, and snippets.

@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
public static TValue GetValueOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dic, TKey key, TValue defaultVal = default(TValue))
{
TValue ret;
return dic.TryGetValue(key, out ret) ? ret : defaultVal;
}
public static TValue GetValueOrDefault<TValue>(this StateBag stateBag, string key, TValue defaultValue = default(TValue))
{
object value = stateBag[key];
return Equals(value, null) ? defaultValue : (TValue) value;
public static IEnumerable<T> Unpage<T>(Func<int, int, IEnumerable<T>> func, int batchSize)
{
int skip = 0;
int count;
do
{
count = 0;
foreach (var a in func(skip, batchSize))
{
count++;