Skip to content

Instantly share code, notes, and snippets.

@huanlin
huanlin / remove-compile-items.xml
Created March 18, 2018 13:13
Remove Compile Items in csproj file.
<ItemGroup>
<Compile Include="TSChineseDictionary.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TSChineseConverter.cs" />
</ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.0-preview1-final" />
<PackageReference Include="Serilog" Version="2.7.1-dev-00960" />
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2" />
<PackageReference Include="Serilog.Sinks.File" Version="4.0.1-dev-00795" />
// 設定 DI
var serviceProvider = new ServiceCollection()
.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true))
.BuildServiceProvider();
// 建立 logger
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.File(opts.LogFileName, Serilog.Events.LogEventLevel.Information)
.CreateLogger();
public class TSChineseDictionary
{
private ILogger _logger;
public TSChineseDictionary(ILogger logger)
{
_logger = logger;
}
}
Log.Information("應用程式開始執行。");
var dict = new TSChineseDictionary(Log.Logger);
@huanlin
huanlin / NukeBuild.cs
Created April 11, 2018 11:58
Nuke Build Code
using System;
using System.Linq;
using Nuke.Common;
using Nuke.Common.Git;
using Nuke.Common.Tools.GitVersion;
using Nuke.Core;
using static Nuke.Common.Tools.DotNet.DotNetTasks;
using static Nuke.Core.IO.FileSystemTasks;
using static Nuke.Core.IO.PathConstruction;
using static Nuke.Core.EnvironmentInfo;
@huanlin
huanlin / old-format.csproj
Last active April 23, 2018 00:51
Upgrading Windows Forms project format to SDK-based format
<ItemGroup>
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
<SubType>Designer</SubType>
@huanlin
huanlin / VeeamBackupAllVMs.ps1
Last active October 4, 2019 14:44
Powershell Script for Automatic Backup with Veeam Backup Free Edition
<#
Utility functions for deleting old files.
Source: http://blog.danskingdom.com/powershell-functions-to-delete-old-files-and-empty-directories/
#>
# Function to remove all empty directories under the given path.
# If -DeletePathIfEmpty is provided the given Path directory will also be deleted if it is empty.
# If -OnlyDeleteDirectoriesCreatedBeforeDate is provided, empty folders will only be deleted if they were created before the given date.
# If -OnlyDeleteDirectoriesNotModifiedAfterDate is provided, empty folders will only be deleted if they have not been written to after the given date.
function Remove-EmptyDirectories([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path, [switch] $DeletePathIfEmpty, [DateTime] $OnlyDeleteDirectoriesCreatedBeforeDate = [DateTime]::MaxValue, [DateTime] $OnlyDeleteDirectoriesNotModifiedAfterDate = [DateTime]::MaxValue, [switch] $OutputDeletedPaths, [switch] $WhatIf)
@huanlin
huanlin / DemoDeadlockController.cs
Last active March 19, 2020 07:15
Demo deadlock with getting async result in ASP.NET 4.x
public class DemoDeadlockController : ApiController
{
[HttpGet]
public HttpResponseMessage DownloadPage()
{
var task = MyDownloadPageAsync("https://www.huanlintalk.com");
var content = task.Result;
return Request.CreateResponse($"網頁長度: {content.Length}");
}