投稿

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=...

シンプルにMSbuildコマンドを使う(Visual Studio2019 Community)

MSBuildを使ってビルドしたのでメモる。 ■ MSBuildのパスは以下の通り。 ↓をWindowsの環境変数に登録する。 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin ■ Release Build msbuild /p:Configuration=Release /t:Rebuild     ■ Publish Build SET MSBuildPath=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\ SET BuildPath=F:\work\DotNetFramework\WebMVC   "%MSBuildPath%\MSBuild.exe" "%BuildPath%\WebMVC.csproj" /t:Build /p:DeployOnBuild=true /p:Configuration=Release /p:TargetFramework=v4.7.2 /p:PublishProfile=FolderProfile /p:RestorePackages=false /p:SkipPostSharp=true   Publish Build の出力先 \WebMVC\Properties\PublishProfiles \ FolderProfile.pubxml を確認すると、 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">   <PropertyGroup>     <DeleteExistingFiles>False</DeleteExistingFiles>     <ExcludeApp_Data>False</ExcludeApp_Data...

ソリューション構成ごとにconfigファイルを作成する

イメージ
.NetFrameworkのソリューション構成ごとにweb.configを作成し、そのconfigファイルの内容を反映させる。 ちょっと、つまずいたのでメモしておく。 (アクション) 1. 新しいソリューション構成を作成する。 2. web.configを作成する。 3. 変換構文の作成 4. .csprojファイルの編集 1. 新しいソリューション構成の作成 Visual Studioの「ビルド」→「構成マネージャー」→「アクティブソリューション構成」→「新規作成」 で、新しいソリューション構成を作成する。 2. web.configの作成 Web.configを右クリックして「Config変換を追加」をクリックすると、先ほど追加した新しいソリューション構成のWeb.configが追加される。 3. 変換構文の作成 前回のブログ を参考に作成する。 <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">     <connectionStrings>         <add name="ConnectionString"           connectionString="Data Source=192.168.44.10.1\SQLEXPRESS;Initial Catalog=AWS_DB;;Connect Timeout=60;Persist Security Info=False; User ID=hoge;Password=1234"           xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>     </connectionStrings>     <appSettings>      ...

web.config debug/releaseの内容を変換構文を使って切り替える

イメージ
.Net Framework のWeb.Debug .config, Web.Release.config の内容を「変換構文」を使って、構成 (Debug/Release) の切り替えによって変えることができたのでメモしておく。   Release 構成でのビルドでは、 Web.config に記載されている要素のうち、以下の3要素を変換構文を使って変更した。 system.web connectionStrings appSettings   1. system.web   compilation debug=true の削除( Release 時は、デバッグ情報は不要) ●Web.configの記述 <system.web>     <compilation debug="true" targetFramework="4.7.2" />     <httpRuntime targetFramework="4.7.2" /> </system.web> 上記から、「debug="true"」を削除するには、 RemoveAttributes を使う。 ●Web.Release.config <system.web>     <!-- Disable Debug Mode -->     <compilation xdt:Transform="RemoveAttributes(debug)" /> </system.web> 2. connectionStrings   DBの接続情報を変更する。 ( Debug / Release時は普通環境変わるだろう ) ●Web.configの記述 <connectionStrings>     <add name="ConnectionString"         connectionString="Data So...

Nlog.configをDebug / Rlease構成で使い分ける

イメージ
ビルド時のソリューション構成で利用するNlog.configを使い分けたい。 最終的に作った構成をメモしておく。 (環境) IDE: Visual Studio Community 2019 Framework: .NetFramework4.5 本当は、 ${buildConfiguration}を使いたいが Nlog に不具合があり、まだ治っていない。 ※(参照: Filter by Build Configuration #1929 ) よって、プリプロセッサを使ってコードに埋め込む。 プロジェクト構成は以下の通り。 NLog.Debug.config NLog.Release.config を使う。 ※NLog.configは存在しているが使わない。 Global.asax.csにNlog.configの読み込みコードを埋め込む。 Global.asax.cs using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace WebMVC {     public class MvcApplication : HttpApplication     {         protected void Application_Start()         {             AreaRegistration.RegisterAllAreas();             FilterConfig.RegisterGlobalFilt...

NLogでLog Parser Studioを使う

イメージ
準備することは以下の通り。 1. ログフォーマットをCSVにする 2. 出力ファイルにヘッダを付けておく 1. ログフォーマットをCSVにする NLogで作成されたログファイルをLog Parser Studio(LPS)で読み込むために、ファイルはCSV形式にしておく。 Nlog.configは以下の通り。layout部分の設定がCSV形式となる。 <targets>     <target xsi:type="File" name="logfile" fileName="./logs/${shortdate}.log"         encoding="UTF-8"         archiveFileName="${basedir}/logs/archives/archive.{#}.log"         archiveEvery="Day"         archiveNumbering="Rolling"         maxArchiveFiles="7"         layout="${longdate},${uppercase:${level}},${callsite},${callsite-linenumber},${message}" /> 2. 出力ファイルにヘッダを付けておく $(shortdate).logを作成する。(ex. 2020-07-23.log)を作成し、ヘッダに 「date, level, class, line, message」とか記載しておく。 プログラムを実行すると、ログは以下の通りとなる。 2020-07-23 17:44:41.2477,DEBUG,WebMVC.Controllers.HomeController.Index,17,d...

.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"  ...