Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
(연관된 글이 1개 있습니다.)
(시리즈 글이 3개 있습니다.)
개발 환경 구성: 220. supportedRuntime 설정을 위한 app.config Transformation
; https://www.sysnet.pe.kr/2/0/1662

.NET Framework: 1193. (appsettings.json처럼) web.config의 Debug/Release에 따른 설정 적용
; https://www.sysnet.pe.kr/2/0/13028

VS.NET IDE: 200. C# - app.config 파일의 출력을 Configuration(Debug/Release)에 따라 제어하는 방법
; https://www.sysnet.pe.kr/2/0/13918




C# - app.config 파일의 출력을 Configuration(Debug/Release)에 따라 제어하는 방법

대표적인 예로, Debug / Release 빌드 시 다른 app.config 출력을 원할 때가 있습니다. 예전에도 이와 관련한 예제를 다루긴 했는데요,

supportedRuntime 설정을 위한 app.config Transformation
; https://www.sysnet.pe.kr/2/0/1662

golavr/ConfigurationTransform
; https://github.com/golavr/ConfigurationTransform

아쉽게도 그때 소개한 Configuration Transform 도구가 비주얼 스튜디오 2022 버전을 지원하지 않습니다. github 프로젝트에 들어가 보면, "This work has been superseded by https://docs.microsoft.com/en-us/dotnet/core/extensions/configuration"라는 문구가 나오는데요, 링크를 따라가 보면 그다지 관련 없는 듯한 내용의 "Configuration in .NET" 글이 나옵니다. 이건 제 추측이지만 아마도 닷넷 코어부터 appsettings.json 방식이 새롭게 도입되면서 XML 방식의 app.config 지원을 스스로 포기한 것이 아닌가 싶습니다.

암튼, 이렇게 되면 본연의 XDT Transforms을 사용하거나,

XDT (web.config) Transforms in non-web projects
; https://devblogs.microsoft.com/dotnet/xdt-web-config-transforms-in-non-web-projects-2/

이것을 쉽게 처리해 주는 (웬일인지 마이크로소프트가 별도로 만든) 확장 도구를 사용하면 됩니다.

SlowCheetah
; https://marketplace.visualstudio.com/items?itemName=vscps.SlowCheetah-XMLTransforms-VS2022

microsoft/slow-cheetah
; https://github.com/Microsoft/slow-cheetah

도구 설치 후, 가령 Console Application 프로젝트를 하나 만들고 app.config 파일을 마우스로 우클릭하면 아래와 같이 "Add Transform" 메뉴가 뜹니다.

278679_1_AddTransform.png

이를 선택하면, 솔루션 탐색기의 app.config 파일 노드 하위로 다음과 같이 2개의 Debug/Release 변형 파일이 생성됩니다.

278680_1_TransformFiles.png

테스트를 위해 3개의 파일을 각각 다음과 같이 구성하고,

[app.config]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="MySetting" value="Hello, World! - (Any)" />
        <add key="DebugSetting" value="1" />
    </appSettings>
</configuration>

[app.Debug.config]
<?xml version="1.0" encoding="utf-8"?>
<!--For more information on using transformations see the web.config examples at http://go.microsoft.com/fwlink/?LinkId=214134. -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <appSettings>
        <add key="MySetting" value="Hello, World! - (Debug)" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
    </appSettings>
</configuration>

[app.Release.config]
<?xml version="1.0" encoding="utf-8"?>
<!--For more information on using transformations see the web.config examples at http://go.microsoft.com/fwlink/?LinkId=214134. -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <appSettings>
        <add key="MySetting" value="Hello, World! - (Release)" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
        <add key="DebugSetting" value="http://contoso.com/" xdt:Transform="Remove" xdt:Locator="Match(key)"/>
    </appSettings>
</configuration>

코드를 작성한 후,

using System.Configuration;

internal class Program
{
    // .NET Core/5+의 경우
    // Install-Package System.Configuration.ConfigurationManager
    static void Main(string[] args)
    {
        string? value = ConfigurationManager.AppSettings["MySetting"];
        Console.WriteLine(value);

        value = ConfigurationManager.AppSettings["DebugSetting"];
        Console.WriteLine(value ?? "(null)");
    }
}

실행해 보면 Debug/Release에 따라 각각 다음과 같은 결과를 얻을 수 있습니다.

// Debug로 빌드한 경우 실행 결과

Hello, World! - (Debug)
1

// Release로 빌드한 경우 실행 결과

Hello, World! - (Release)
(null)

(첨부 파일은 이 글의 예제 코드를 포함합니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 5/2/2025]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...
NoWriterDateCnt.TitleFile(s)
13621정성태5/10/20248699오류 유형: 903. IISExpress - Failed to register URL "..." for site "..." application "/". Error description: Cannot create a file when that file already exists. (0x800700b7)
13620정성태5/9/20248601VS.NET IDE: 190. Visual Studio가 node.exe를 경유해 Edge.exe를 띄우는 경우
13619정성태5/7/20248899닷넷: 2259. C# - decimal 저장소의 비트 구조파일 다운로드1
13618정성태5/6/20248676닷넷: 2258. C# - double (배정도 실수) 저장소의 비트 구조파일 다운로드1
13617정성태5/5/20249542닷넷: 2257. C# - float (단정도 실수) 저장소의 비트 구조파일 다운로드1
13616정성태5/3/20248679닷넷: 2256. ASP.NET Core 웹 사이트의 HTTP/HTTPS + Dual mode Socket (IPv4/IPv6) 지원 방법파일 다운로드1
13615정성태5/3/20249581닷넷: 2255. C# 배열을 Numpy ndarray 배열과 상호 변환
13614정성태5/2/20249263닷넷: 2254. C# - COM 인터페이스의 상속 시 중복으로 메서드를 선언
13613정성태5/1/20248971닷넷: 2253. C# - Video Capture 장치(Camera) 열거 및 지원 포맷 조회파일 다운로드1
13612정성태4/30/20249366오류 유형: 902. Visual Studio - error MSB3021: Unable to copy file
13611정성태4/29/20248632닷넷: 2252. C# - GUID 타입 전용의 UnmanagedType.LPStruct - 두 번째 이야기파일 다운로드1
13610정성태4/28/20249092닷넷: 2251. C# - 제네릭 인자를 가진 타입을 생성하는 방법 - 두 번째 이야기
13609정성태4/27/20249220닷넷: 2250. PInvoke 호출 시 참조 타입(class)을 마샬링하는 [IN], [OUT] 특성파일 다운로드1
13608정성태4/26/20249557닷넷: 2249. C# - 부모의 필드/프로퍼티에 대해 서로 다른 자식 클래스 간에 Reflection 접근이 동작할까요?파일 다운로드1
13607정성태4/25/20249566닷넷: 2248. C# - 인터페이스 타입의 다중 포인터를 인자로 갖는 C/C++ 함수 연동
13606정성태4/24/20249411닷넷: 2247. C# - tensorflow 연동 (MNIST 예제)파일 다운로드1
13605정성태4/23/202410784닷넷: 2246. C# - Python.NET을 이용한 파이썬 소스코드 연동파일 다운로드1
13604정성태4/22/20248494오류 유형: 901. Visual Studio - Unable to set the next statement. Set next statement cannot be used in '[Exception]' call stack frames.
13603정성태4/21/20249772닷넷: 2245. C# - IronPython을 이용한 파이썬 소스코드 연동파일 다운로드1
13602정성태4/20/20249038닷넷: 2244. C# - PCM 오디오 데이터를 연속(Streaming) 재생 (Windows Multimedia)파일 다운로드1
13601정성태4/19/20249148닷넷: 2243. C# - PCM 사운드 재생(NAudio)파일 다운로드1
13600정성태4/18/20249812닷넷: 2242. C# - 관리 스레드와 비관리 스레드
13599정성태4/17/20249644닷넷: 2241. C# - WAV 파일의 PCM 사운드 재생(Windows Multimedia)파일 다운로드1
13598정성태4/16/202410806닷넷: 2240. C# - WAV 파일 포맷 + LIST 헤더파일 다운로드2
13597정성태4/15/20249176닷넷: 2239. C# - WAV 파일의 PCM 데이터 생성 및 출력파일 다운로드1
13596정성태4/14/20249794닷넷: 2238. C# - WAV 기본 파일 포맷파일 다운로드1
1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...