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)
13673정성태7/11/20248550닷넷: 2275. C# 13 - (1) 신규 이스케이프 시퀀스 '\e'파일 다운로드1
13672정성태7/10/20247200닷넷: 2274. IIS - (프로세스 종료 없는) AppDomain Recycle
13671정성태7/10/20247290오류 유형: 914. Package ca-certificates is not installed.
13669정성태7/9/20247410오류 유형: 913. C# - AOT StaticExecutable 정적 링킹 시 빌드 오류
13668정성태7/8/20247437개발 환경 구성: 716. Hyper-V - Ubuntu 22.04 Generation 2 유형의 VM 설치
13667정성태7/7/20246643닷넷: 2273. C# - 리눅스 환경에서의 Hyper-V Socket 연동 (AF_VSOCK)파일 다운로드1
13666정성태7/7/20247763Linux: 74. C++ - Vsock 예제 (Hyper-V Socket 연동)파일 다운로드1
13665정성태7/6/20247963Linux: 73. Linux 측의 socat을 이용한 Hyper-V 호스트와의 vsock 테스트파일 다운로드1
13663정성태7/5/20247593닷넷: 2272. C# - Hyper-V Socket 통신(AF_HYPERV, AF_VSOCK)의 VMID Wildcards 유형파일 다운로드1
13662정성태7/4/20247576닷넷: 2271. C# - WSL 2 VM의 VM ID를 알아내는 방법 - Host Compute System API파일 다운로드1
13661정성태7/3/20247468Linux: 72. g++ - 다른 버전의 GLIBC로 소스코드 빌드
13660정성태7/3/20247642오류 유형: 912. Visual C++ - Linux 프로젝트 빌드 오류
13659정성태7/1/20247929개발 환경 구성: 715. Windows - WSL 2 환경의 Docker Desktop 네트워크
13658정성태6/28/20248342개발 환경 구성: 714. WSL 2 인스턴스와 호스트 측의 Hyper-V에 운영 중인 VM과 네트워크 연결을 하는 방법 - 두 번째 이야기
13657정성태6/27/20247995닷넷: 2270. C# - Hyper-V Socket 통신(AF_HYPERV, AF_VSOCK)을 위한 EndPoint 사용자 정의
13656정성태6/27/20248217Windows: 264. WSL 2 VM의 swap 파일 위치
13655정성태6/24/20247972닷넷: 2269. C# - Win32 Resource 포맷 해석파일 다운로드1
13654정성태6/24/20247849오류 유형: 911. shutdown - The entered computer name is not valid or remote shutdown is not supported on the target computer.
13653정성태6/22/20247978닷넷: 2268. C# 코드에서 MAKEINTREOURCE 매크로 처리
13652정성태6/21/20249317닷넷: 2267. C# - Linux 환경에서 (Reflection 없이) DLL AssemblyFileVersion 구하는 방법파일 다운로드2
13651정성태6/19/20248548닷넷: 2266. C# - (Reflection 없이) DLL AssemblyFileVersion 구하는 방법파일 다운로드1
13650정성태6/18/20248503개발 환경 구성: 713. "WSL --debug-shell"로 살펴보는 WSL 2 VM의 리눅스 환경
13649정성태6/18/20248047오류 유형: 910. windbg - !py 확장 명령어 실행 시 "failed to find python interpreter" (2)
13648정성태6/17/20248418오류 유형: 909. C# - DynamicMethod 사용 시 System.TypeAccessException
13647정성태6/16/20249429개발 환경 구성: 712. Windows - WSL 2의 네트워크 통신 방법 - 세 번째 이야기 (같은 IP를 공유하는 WSL 2 인스턴스) [1]
13646정성태6/14/20247909오류 유형: 908. Process Explorer - "Error configuring dump resources: The system cannot find the file specified."
1  2  3  4  5  6  7  8  9  10  [11]  12  13  14  15  ...