投稿

7月, 2020の投稿を表示しています

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

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 APIでS3ファイル(apk)をダウンロードする

AWS S3ライブラリーを使って、AWS S3のバケットからAPKファイルをダウンロードしたのでメモしておく。 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Mime; using System.Threading.Tasks; using Amazon; using Amazon.S3; using Amazon.S3.Model; using Microsoft.AspNetCore.Mvc; namespace WebApi.Controllers { [ApiController] [Route("api/[controller]")] // [Route("[controller]")] public class S3Controller : ControllerBase { private const string bucketName = "hoge"; // private const string keyName = "hoge.jpg"; private const string keyName = "HelloWorld.apk"; // Specify your bucket region (an example region is shown). private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USEast2; private static IAmazonS3 client; // APKをダウンロード [HttpGet("apk")] public async Task<IActionResult> GetApkFileAsync() { client = new AmazonS3Client(bucketRegion); //string res = await ReadObjectDataAsync(); //System.Net...

S3 BucketへのアクセスをPlicyで制御する

S3のファイルアクセスについて、メモしておく。 したいこと ・Bucketにあるファイルだけにアクセスする。 ・ユーザーはだれでもいい。 ・特定のIPAddressにのみ権限を与える。 すること 1. バケットの制御 一旦、ブロックパブリックアクセスで全て公開した、バケットポリシーで制限する。 ・ブロックパブリックアクセス  →全てオフ(ブロックしない。公開する。) ・アクセスコントロールリスト  →何もしない。 ・バケットポリシー  →以下の設定を行う。 S3 Policy { "Version": "2012-10-17", "Id": "Policy2222222", "Statement": [ { "Sid": "StmtNotAccess", "Effect": "Deny", "Principal": "*", "Action": "*", "Resource": "arn:aws:s3:::<backetname>/*", "Condition": { "NotIpAddress": { "aws:SourceIp": "XXX.XXX.XXX.XXX/32" } } }, { "Sid": "Stmt1594450691768", "Effect": "Allow", ...

BATファイルでSeleniumを動かす

勉強中。 @echo off set COMMAND=selenium-side-runner F:\selenium\OsakaGas.side -c "browserName=firefox" set FINENAME=%date:~0,4%%date:~5,2%%date:~8,2% SET BAT_LOG=%~dp0log\%FINENAME%.log rem %~dp0%COMMAND% > %BAT_LOG% 2>&1 %COMMAND% > %BAT_LOG% 2>&1 rem echo 標準出力とエラー出力が表示されます。 >> %BAT_LOG% 2>&1 rem Pause rem Exit 参考 バッチファイルのログ作成方法

seleniumをコマンドで起動させる

Package Install $ npm install -g selenium-side-runner $ npm install -g chromedriver $ npm install -g edgedriver $ npm install -g geckodriver まずは、何か起動させてみる。 $ selenium-side-runner F:\selenium\yahoo_test.side info: Running F:/selenium/yahoo_test.side FAIL ./DefaultSuite.test.js ● Test suite failed to run The ChromeDriver could not be found on the current PATH. Please download the latest version of the ChromeDriver from http://chromedriver.storage.googleapis.com/index.html and ensure it can be found on your PATH. ------ Test Suites: 1 failed, 1 total Tests: 0 total Snapshots: 0 total Time: 0.301s Ran all test suites. エラー発生。Chrome Driverのパスの問題。 エラー対応。 PATHが通っていないので通す。 $ set PATH=%PATH%;C:\Users\kumag\AppData\Local\Temp\83.0.4103.39\chromedriver $ selenium-side-runner F:\selenium\yahoo_test.side これでOK。 FirefoxならInstall時にPATHが通っている。ChromeのPATHを通すのがめんどくさいならこちらにしましょう。 $ selenium-side-runner F:\selenium\OsakaGas.side -c "browserName=firefox" バックグラウンド実...

CentOS7でJenkinsを立ち上げる

必要な手順は以下の通り。 1. JDKのインストール 2. Jenkinsのインストール これだけでいい。 Tomcatも必要だと思っていたが、JenkinsにはWinstoneという軽量コンテナが埋め込まれているので何もなくても起動する。 環境確認 cat /etc/redhat-release CentOS Linux release 7.8.2003 (Core) 1. JDKのインストール rpmは ここ から取得する。 $ sudo yum localinstall jdk-11.0.7_linux-x64_bin.rpm $ echo "export JAVA_HOME=/usr/java/jdk-11.0.7/bin/java" >> ~/.bash_profile 最新バージョンではない理由は、最新であるVer14をインストールし、Jenkinsを起動すると以下のエラーが発生した。 Jenkinsの起動でエラー発生 Jenkins requires Java versions [8, 11] but you are running with Java 14 from /usr/java/jdk-14.0.1 つまり、Ver8~11にせよ、ということ。 で仕方がないので、Ver11をインストールしてver11を選択している。 $ sudo alternatives --config java There are 2 programs which provide 'java'. Selection Command ----------------------------------------------- * 1 /usr/java/jdk-14.0.1/bin/java + 2 /usr/java/jdk-11.0.7/bin/java Enter to keep the current selection[+], or type selection number: 2 2. Jenkinsのインストール これは簡単。 $ sudo wget -O /etc/yum.repos.d/jenkins.repo https...