<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>windows 彙整 - 泰克哪裡去</title>
	<atom:link href="https://tech.uccu.website/tag/windows/feed" rel="self" type="application/rss+xml" />
	<link>https://tech.uccu.website/tag/windows</link>
	<description>一個科技相關的隨手記錄網站</description>
	<lastBuildDate>Sat, 28 Aug 2021 15:39:24 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
<site xmlns="com-wordpress:feed-additions:1">119574712</site>	<item>
		<title>Windows Container中修改json檔案設定值的方法</title>
		<link>https://tech.uccu.website/how-to-modify-json-file-in-windows-container.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-modify-json-file-in-windows-container</link>
					<comments>https://tech.uccu.website/how-to-modify-json-file-in-windows-container.html#respond</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Tue, 24 Nov 2020 09:28:50 +0000</pubDate>
				<category><![CDATA[Windows]]></category>
		<category><![CDATA[appsettings.json]]></category>
		<category><![CDATA[container]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[windows]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=153</guid>

					<description><![CDATA[<p>通常使用到Docker技術執行Container容器都是使用Linux環境，但是Windows環境也可以使用C ... <a title="Windows Container中修改json檔案設定值的方法" class="read-more" href="https://tech.uccu.website/how-to-modify-json-file-in-windows-container.html" aria-label="Read more about Windows Container中修改json檔案設定值的方法">閱讀全文</a></p>
<p>這篇文章 <a href="https://tech.uccu.website/how-to-modify-json-file-in-windows-container.html">Windows Container中修改json檔案設定值的方法</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>通常使用到Docker技術執行Container容器都是使用Linux環境，但是Windows環境也可以使用Container，有的時候因為一些考量會需要製作兩套不同環境(Windows &amp; Linux)的Docker Image，所以前面一篇文章提到了如何在<em><a href="https://tech.uccu.website/how-to-modify-json-file-in-linux-container.html" target="_blank" rel="noreferrer noopener">Linux Container中修改json檔案設定值的方法</a></em>，這一篇則是來說明如何在Windows Container中做到一樣的事情。</p>



<p>不同的地方在於，Linux環境中透過額外安裝jq套件來達成修改json檔案的作法，在Windows環境下我們則是使用PowerShell來達成這樣的事情，所以不需要額外安裝任何套件(PowerShell應該都有內建在Windows環境中)。</p>



<span id="more-153"></span>



<p>下面的內容和<em><a href="https://tech.uccu.website/how-to-modify-json-file-in-linux-container.html" target="_blank" rel="noreferrer noopener">Linux Container中修改json檔案設定值的方法</a></em>文章中的內容幾乎一樣，我額外用不同的字型顏色標出不同的地方，以便參考。</p>



<h2 class="wp-block-heading">使用方式</h2>



<p>假設在Dockerfile中定義了<em><strong>ENV&nbsp;LOG_LEVEL=Error</strong></em>，並且appsettings.json檔案中有段Serilog的設定如下：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-json">{
    &quot;Serilog&quot;: {
        &quot;MinimumLevel&quot;: &quot;Debug&quot;,
        &quot;WriteTo&quot;: [
            {
                &quot;Name&quot;: &quot;File&quot;,
                &quot;Args&quot;: {
                    &quot;path&quot;: &quot;MyApp.log&quot;,
                    &quot;outputTemplate&quot;: &quot;{Timestamp:yyyy-MM-dd HH:mm:ss} RequestID:{RequestID} User:{UserID} {RequestPath} {SourceContext} [{Level:u3}] {Message:lj}{NewLine}{Exception}&quot;,
                    &quot;rollingInterval&quot;: &quot;Day&quot;
                }
            }
        ],
        &quot;Enrich&quot;: [ &quot;FromLogContext&quot; ]
    }
}</code></pre>



<p>可以看到在Serilog中有個MinimumLevel屬性設定的是Debug，這個設定會記錄大量的Log記錄，但是有時候在執行container的時候並不想要記錄這麼詳細的資訊，以降低檔案大小，減少儲存空間的耗用時，有個LOG_LEVEL的ENV設定就可以方便的調整設定值。</p>



<p>現在假設有個.Net Core的程式叫MyApp，Build完之後有個MyApp.dll檔案，在Dockerfile中設定了ENV和複製相關的執行檔案之外，還需要一個<span style="color:#0000ff" class="has-inline-color">.ps1檔案(Powershell的副檔名)</span>，假設這個檔案叫做app-init.<span style="color:#0000ff" class="has-inline-color">ps1</span>。</p>



<p>接下來在app-init<span style="color:#0000ff" class="has-inline-color">.ps1</span>檔案中要做下面這幾件事：</p>



<ol class="wp-block-list"><li>判斷container是不是第一次執行，以避免每次都進行修改appsettings.json的動作。</li><li>透過<span style="color:#0000ff" class="has-inline-color">Powershell的Get-Content指令</span>將appsettings.json的內容讀入記憶體。</li><li>找到MinimumLevel屬性，將值修改為LOG_LEVEL這個ENV所設定的值。</li><li>將<span style="color:#0000ff" class="has-inline-color">Powershell</span>修改後的內容儲存為appsettings.temp.json。</li><li>將原本的appsettings.json改名為appsettings.ori.json。</li><li>將前面的appsettings.temp.json改名為appsettings.json。</li><li>產生一個用來判斷是否執行過app-init<span style="color:#0000ff" class="has-inline-color">.ps1</span>的檔案，例如：inited<span style="color:#0000ff" class="has-inline-color">.txt</span>。</li><li>啟動MyApp。</li></ol>



<pre class="wp-block-prismatic-blocks"><code class="language-powershell">Set-Location C:\app

if (Test-Path inited.txt) {
    echo &quot;Powershell already run finished.&quot;
}
else {
    $JsonSettings = Get-Content appsettings.json | ConvertFrom-Json
    $JsonSettings.Serilog.MinimumLevel = &quot;$env:LOG_LEVEL&quot;
    $JsonSettings | ConvertTo-Json -Depth 10 &gt; appsettings.temp.json

    Rename-Item appsettings.json appsettings.ori.json
    Rename-Item appsettings.temp.json appsettings.json
    &quot;Powershell run finished.&quot; &gt; inited.txt
}

dotnet MyApp.dll</code></pre>
<p>這篇文章 <a href="https://tech.uccu.website/how-to-modify-json-file-in-windows-container.html">Windows Container中修改json檔案設定值的方法</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tech.uccu.website/how-to-modify-json-file-in-windows-container.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">153</post-id>	</item>
	</channel>
</rss>
