@@ -30,13 +30,16 @@ sealed class ProxyEngine(
3030 IProxyConfiguration proxyConfiguration ,
3131 ISet < UrlToWatch > urlsToWatch ,
3232 IProxyStateController proxyController ,
33- ILogger < ProxyEngine > logger ) : BackgroundService , IDisposable
33+ ILogger < ProxyEngine > logger ,
34+ ILoggerFactory loggerFactory ) : BackgroundService , IDisposable
3435{
3536 private readonly IEnumerable < IPlugin > _plugins = plugins ;
3637 private readonly ILogger _logger = logger ;
3738 private readonly IProxyConfiguration _config = proxyConfiguration ;
3839
39- internal static ProxyServer ProxyServer { get ; private set ; }
40+ internal static ProxyServer ProxyServer { get ; private set ; } = null ! ;
41+ private static bool _isProxyServerInitialized ;
42+ private static readonly object _initLock = new ( ) ;
4043 private ExplicitProxyEndPoint ? _explicitEndPoint ;
4144 // lists of URLs to watch, used for intercepting requests
4245 private readonly ISet < UrlToWatch > _urlsToWatch = urlsToWatch ;
@@ -56,23 +59,49 @@ sealed class ProxyEngine(
5659
5760 static ProxyEngine ( )
5861 {
59- ProxyServer = new ( ) ;
60- ProxyServer . CertificateManager . PfxFilePath = Environment . GetEnvironmentVariable ( "DEV_PROXY_CERT_PATH" ) ?? string . Empty ;
61- ProxyServer . CertificateManager . RootCertificateName = "Dev Proxy CA" ;
62- ProxyServer . CertificateManager . CertificateStorage = new CertificateDiskCache ( ) ;
63- // we need to change this to a value lower than 397
64- // to avoid the ERR_CERT_VALIDITY_TOO_LONG error in Edge
65- ProxyServer . CertificateManager . CertificateValidDays = 365 ;
66-
67- using var joinableTaskContext = new JoinableTaskContext ( ) ;
68- var joinableTaskFactory = new JoinableTaskFactory ( joinableTaskContext ) ;
69- _ = joinableTaskFactory . Run ( async ( ) => await ProxyServer . CertificateManager . LoadOrCreateRootCertificateAsync ( ) ) ;
62+ // ProxyServer initialization moved to EnsureProxyServerInitialized
63+ // to enable passing ILoggerFactory for Unobtanium logging
64+ }
65+
66+ // Ensure ProxyServer is initialized with the given ILoggerFactory
67+ // This method can be called from multiple places (ProxyEngine, CertCommand, etc.)
68+ internal static void EnsureProxyServerInitialized ( ILoggerFactory ? loggerFactory = null )
69+ {
70+ if ( _isProxyServerInitialized )
71+ {
72+ return ;
73+ }
74+
75+ lock ( _initLock )
76+ {
77+ if ( _isProxyServerInitialized )
78+ {
79+ return ;
80+ }
81+
82+ ProxyServer = new ( loggerFactory : loggerFactory ) ;
83+ ProxyServer . CertificateManager . PfxFilePath = Environment . GetEnvironmentVariable ( "DEV_PROXY_CERT_PATH" ) ?? string . Empty ;
84+ ProxyServer . CertificateManager . RootCertificateName = "Dev Proxy CA" ;
85+ ProxyServer . CertificateManager . CertificateStorage = new CertificateDiskCache ( ) ;
86+ // we need to change this to a value lower than 397
87+ // to avoid the ERR_CERT_VALIDITY_TOO_LONG error in Edge
88+ ProxyServer . CertificateManager . CertificateValidDays = 365 ;
89+
90+ using var joinableTaskContext = new JoinableTaskContext ( ) ;
91+ var joinableTaskFactory = new JoinableTaskFactory ( joinableTaskContext ) ;
92+ _ = joinableTaskFactory . Run ( async ( ) => await ProxyServer . CertificateManager . LoadOrCreateRootCertificateAsync ( ) ) ;
93+
94+ _isProxyServerInitialized = true ;
95+ }
7096 }
7197
7298 protected override async Task ExecuteAsync ( CancellationToken stoppingToken )
7399 {
74100 _cancellationToken = stoppingToken ;
75101
102+ // Initialize ProxyServer with LoggerFactory for Unobtanium logging
103+ EnsureProxyServerInitialized ( loggerFactory ) ;
104+
76105 Debug . Assert ( ProxyServer is not null , "Proxy server is not initialized" ) ;
77106
78107 if ( ! _urlsToWatch . Any ( ) )
0 commit comments