投稿

ラベル(log)が付いた投稿を表示しています

Dotnet FrameworkでSystem.Diagnostics.ConsoleTraceListenerを使う

Console Applicationを作成する。 App.configは以下の通り。 <configuration > <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" /> </startup> <system.diagnostics> <sources> <source name="TraceTest" switchName="sourceSwitch" switchType="System.Diagnostics.SourceSwitch"> <listeners> <add name="Console"/> <add name="myListener"/> <remove name="Default"/> </listeners> </source> </sources> <switches> <!--<add name="sourceSwitch" value="Warning"/>--> <!--<add name="sourceSwitch" value="Information"/>--> <add name="sourceSwitch" value="Verbose"/> </switches> <sharedListeners> <add name="myListener" type="System.Diagnostics.TextWriterTraceL...

MSbuildコマンドをバッチファイルで実行し、ログを出力する(Visual Studio2019 Community)

JenkinsでMSBuildを実行するためにバッチファイルを作成したのでメモしておく。 引数には、「ソリューション構成」を設定。 エラーログを日付を入れたファイルで出力させる。 @echo off rem ログファイルに日付を入れる set dt1=%date:~0,4%%date:~5,2%%date:~8,2% set tm1=%time:~0,2%%time:~3,2%%time:~6,2% set fname1=%dt1%_%tm1% rem set WORK_DIR=%~dp0 set WORK_DIR=F:\svn\trunk pushd %WORK_DIR% rem ログ出力先 set OUTPUTS=%WORK_DIR%\..\publish rem ソリューション構成の設定(引数設定) set CONFIG=%~1 if "%CONFIG%"=="" set CONFIG=Release If not exist %OUTPUTS%\%CONFIG% mkdir -p %OUTPUTS%\%CONFIG% SET MSBuildPath=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\ SET BuildPath=F:\svn\trunk "%MSBuildPath%\MSBuild.exe" "%BuildPath%\WebMVC.csproj" ^ /t:WebPublish /p:Configuration=%CONFIG% ^ /p:WebPublishMethod=FileSystem ^ /p:publishUrl=%OUTPUTS%\%CONFIG% ^ -fl1 -fl2 -fl3 ^ -flp2:logfile=%OUTPUTS%\JustErrors-%dt1%_%tm1%.log;errorsonly ^ -flp3:logfile=%OUTPUTS%\JustWarnings-%dt1%_%tm1%.log;warningsonly if %errorlevel% neq 0 ( ech...

web.configにNlog設定を踏襲し、変換構文を使ってソリューション構成ごとに異なるログ設定をする

アクション 最初に以下を宣言する。 これで、ソースコードにはログを貼るだけでよくなる。   <configSections>     <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>   </configSections> あとは、targetとruleを付けるだけ。 全体像は以下の通り。 Web.config <configuration>   <configSections>     <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>   </configSections>   <nlog>     <targets>       <target name="logFile" type="File" fileName="./logs/${shortdate}-debug.log" encoding="UTF-8"         archiveFileName="${basedir}/logs/archives/archive.${shortdate}-debug.log" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="365"         layout="${longdate},${uppercase:${level}},${callsite},${callsite-linenumber},${message}"/>       <!--       <target name=...

.NetFrameworkでNlog(Nlog.config)を使う

.NetFramework4.5でNlogを使ってみた。 サンプルアプリは、 CreateASP.NET MVC Application を作った。 IDEはVisual Studio2019 手順は以下の通り。 1. NugetパッケージマネージャーでNlogをインストールする 2. NLog.configを設定する 3. ターゲットClassにコーディングする 1. NugetパッケージマネージャーでNlogをインストールする インストールするパッケージは以下の通り。 NLog NLog.Config NLog.Schema(NLog.Configと同時にインストールされる) 2. NLog.configを設定する NLog.configは以下の通り <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"       autoReload="true"       throwExceptions="false"       internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">     <targets>         <target xsi:type="File" name="logfile" fileName="./logs/${shortdate}.log"  ...

Dotnet Core2.2 でSerilogを使う

■ .net core2.1 は、Microsoft.Extensions.Loggingの AddFile が使えた。 public void ConfigureServices(IServiceCollection services) { services.AddLogging(lb => { lb.AddConfiguration(Configuration.GetSection("Logging")); lb.AddFile(o => o.RootPath = AppContext.BaseDirectory); }); } ■ .net Core2.2 は、まだ実装されていないので、サードパーティのプラグインを利用する必要があるようだ。 なので「Seri l og」を利用する。 必要なモジュール Serilog. AspNetCore(dotnet core 用 ) Serilog.Sinks.Trace( Console Window への出力) Serilog.Sinks.File(File 出力 ) Serilog. Sinks.Console(Console 出力 ) Serilog.Sinks.RollingFile( ファイル出力を日付単位等分割する ) Serilog.Settings.Configuration(appsettings.json 用 ) appsettings.json { "Serilog": { "Using": [ "Serilog.Sinks.Console" ], "MinimumLevel": "Debug", "Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ], "WriteTo": [ { ...

dotnet coreでLoggingする

dotnet coreのログ取得方法を勉強する。 やり方は以下の通りとする。 プロジェクトは 前回 のをそのまま流用する appsettings.jsonにログ設定を記載する controllerで出力させる。 1. appsettings.jsonの修正 デフォルトは「すべてのプロバイダー用」である"LogLevel"で設定すればよいのだが、namespace個別にログレベルを設定する場合は「Debugプロバイダー用」を使う。 { "TestUpdateData": { "EndPointUrl": "http://localhost:8080", "AuthorizationKey": "12345678" }, // Debug プロバイダー用 "Logging": { "LogLevel": { "Default": "Debug", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information", "WebApplication4.Controllers": "Debug" } }, // Console プロバイダー用 "Console": { "IncludeScopes": false, "LogLevel": { "Microsoft.AspNetCore.Mvc.Razor.Internal": "Warning", "Microsoft.AspNetCore.Mvc.Razor.Razor": "Debug", "Microsoft.AspNetCor...