<?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>appsettings.json 彙整 - 泰克哪裡去</title>
	<atom:link href="https://tech.uccu.website/tag/appsettings-json/feed" rel="self" type="application/rss+xml" />
	<link>https://tech.uccu.website/tag/appsettings-json</link>
	<description>一個科技相關的隨手記錄網站</description>
	<lastBuildDate>Sat, 28 Aug 2021 15:42:05 +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>
		<item>
		<title>Linux container中修改json檔案設定值的方法</title>
		<link>https://tech.uccu.website/how-to-modify-json-file-in-linux-container.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-modify-json-file-in-linux-container</link>
					<comments>https://tech.uccu.website/how-to-modify-json-file-in-linux-container.html#respond</comments>
		
		<dc:creator><![CDATA[鳴人]]></dc:creator>
		<pubDate>Tue, 24 Nov 2020 09:10:21 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[appsettings.json]]></category>
		<category><![CDATA[container]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[linux]]></category>
		<guid isPermaLink="false">https://tech.uccu.website/?p=151</guid>

					<description><![CDATA[<p>現在許多的程式設計都使用了json格式的檔案作為設定檔的儲存格式，可以跨平台在Linux環境執行的.Net C ... <a title="Linux container中修改json檔案設定值的方法" class="read-more" href="https://tech.uccu.website/how-to-modify-json-file-in-linux-container.html" aria-label="Read more about Linux container中修改json檔案設定值的方法">閱讀全文</a></p>
<p>這篇文章 <a href="https://tech.uccu.website/how-to-modify-json-file-in-linux-container.html">Linux container中修改json檔案設定值的方法</a> 最早出現於 <a href="https://tech.uccu.website">泰克哪裡去</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>現在許多的程式設計都使用了json格式的檔案作為設定檔的儲存格式，可以跨平台在Linux環境執行的.Net Core也不例外，伴隨著appsettings.json檔案中許多的設定，不管是預設的設定，或是不同開發需求自行加入進去的設定值，全部都是json格式。</p>



<p>為了在docker啟動container的時候可以根據繫結的環境參數(environment, -e)值做出相對應的修改，因此需要有個方便的作法可以在container執行的時候動態修改json檔案。</p>



<p>這一篇文章就來介紹如何在Linux中透過jq套件修改json文件。</p>



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



<h2 class="wp-block-heading">先決條件</h2>



<p>Container中需要先安裝jq套件，所以必須在Dockerfile中先透過<em>apt-get install -y jq</em>指令(Ubuntu/Debian)安裝jq套件，以便Docker Image中有jq套件可以使用。</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和複製相關的執行檔案之外，還需要一個.sh檔案(Linux中的Shell檔，相當於Windows的.cmd或.bat)，假設這個檔案叫做app-init.sh。</p>



<p>接下來在app-init.sh檔案中要做下面這幾件事：</p>



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



<p>假設Dockerfile中設定的WORKDIR是/app，第7點的檔案是放在/app/inited，那麼app-init.sh的程式碼內容大概會是下面這個樣子：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-bash">if [ -f &quot;/app/inited&quot; ]; then
    echo &quot;Container inited, skip this shell.&quot;
else
    cat appsettings.json #這行只是先輸出原本的設定值，用來查看用的
    jq &#039;.Serilog.MinimumLevel=env.LOG_LEVEL&#039; appsettings.json &gt; appsettings.temp.json

    cat appsettings.temp.json #查看儲存的檔案內容

    mv appsettings.json appsettings.ori.json #第5點，將原本的設定檔改名
    mv appsettings.temp.json appsettings.json #第6點，將jq儲存的暫存檔改為正式檔名

    echo &quot;Container inited.&quot; &gt; /app/inited #第7點，產生判斷是否init過的檔案
fi

dotnet MyApp.dll #第8點，啟動MyApp</code></pre>



<p>透過上面的app-init.sh檔案，在Container第一次執行的時候就會根據設定的內容修改appsettings.json檔案，產生的inited檔案可以避免container restart之後重複執行init的動作。</p>



<p>透過這樣的技巧，也可以修改appsettings.json中的資料庫連線字串，如果是像Serilog底下的WriteTo屬性是陣列(集合)型式的，則是用[0]、[1]這樣的方式設定，範例如下：</p>



<pre class="wp-block-prismatic-blocks"><code class="language-bash">jq &#039;.Serilog.MinimumLevel=env.LOG_LEVEL&#039; appsettings.json | \
jq &#039;.Serilog.WriteTo[0].Args.rollingInterval=env.LOG_ROLLING_INTERVAL&#039;  &gt; appsettings.temp.json</code></pre>
<p>這篇文章 <a href="https://tech.uccu.website/how-to-modify-json-file-in-linux-container.html">Linux 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-linux-container.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">151</post-id>	</item>
	</channel>
</rss>
