16
16
using System . Diagnostics . CodeAnalysis ;
17
17
using System . IO ;
18
18
using System . Linq ;
19
- using System . ServiceProcess ;
20
19
using System . Text ;
21
- using SeqCli . Forwarder . Util ;
20
+ using Serilog ;
22
21
23
- namespace SeqCli . Forwarder . Util
22
+ namespace SeqCli . Forwarder . Util ;
23
+
24
+ [ SuppressMessage ( "Interoperability" , "CA1416:Validate platform compatibility" ) ]
25
+ public static class ServiceConfiguration
24
26
{
25
- [ SuppressMessage ( "Interoperability" , "CA1416:Validate platform compatibility" ) ]
26
- public static class ServiceConfiguration
27
+ static bool GetServiceCommandLine ( string serviceName , [ MaybeNullWhen ( false ) ] out string path )
27
28
{
28
- public static bool GetServiceBinaryPath ( ServiceController controller , [ MaybeNullWhen ( false ) ] out string path )
29
- {
30
- var sc = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . System ) , "sc.exe" ) ;
31
-
32
- var config = new StringBuilder ( ) ;
33
- if ( 0 != CaptiveProcess . Run ( sc , "qc \" " + controller . ServiceName + "\" " , l => config . AppendLine ( l ) , Console . WriteLine ) )
34
- {
35
- Console . WriteLine ( "Could not query service path; ignoring." ) ;
36
- path = null ;
37
- return false ;
38
- }
39
-
40
- var lines = config . ToString ( )
41
- . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . RemoveEmptyEntries )
42
- . Select ( l => l . Trim ( ) ) ;
43
-
44
- var line = lines
45
- . SingleOrDefault ( l => l . StartsWith ( "BINARY_PATH_NAME : " ) ) ;
46
-
47
- if ( line == null )
48
- {
49
- Console . WriteLine ( "No existing binary path could be determined." ) ;
50
- path = null ;
51
- return false ;
52
- }
29
+ var sc = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . System ) , "sc.exe" ) ;
53
30
54
- path = line . Replace ( "BINARY_PATH_NAME : " , "" ) ;
55
- return true ;
56
- }
57
-
58
- static bool GetServiceCommandLine ( string serviceName , [ MaybeNullWhen ( false ) ] out string path )
31
+ var config = new StringBuilder ( ) ;
32
+ if ( 0 != CaptiveProcess . Run ( sc , "qc \" " + serviceName + "\" " , l => config . AppendLine ( l ) , Console . WriteLine ) )
59
33
{
60
- if ( serviceName == null ) throw new ArgumentNullException ( nameof ( serviceName ) ) ;
61
-
62
- var sc = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . System ) , "sc.exe" ) ;
34
+ Log . Debug ( "Could not query service path; ignoring." ) ;
35
+ path = null ;
36
+ return false ;
37
+ }
63
38
64
- var config = new StringBuilder ( ) ;
65
- if ( 0 != CaptiveProcess . Run ( sc , "qc \" " + serviceName + "\" " , l => config . AppendLine ( l ) , Console . WriteLine ) )
66
- {
67
- Console . WriteLine ( "Could not query service path; ignoring." ) ;
68
- path = null ;
69
- return false ;
70
- }
39
+ var lines = config . ToString ( )
40
+ . Split ( [ Environment . NewLine ] , StringSplitOptions . RemoveEmptyEntries )
41
+ . Select ( l => l . Trim ( ) ) ;
71
42
72
- var lines = config . ToString ( )
73
- . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . RemoveEmptyEntries )
74
- . Select ( l => l . Trim ( ) ) ;
43
+ var line = lines
44
+ . SingleOrDefault ( l => l . StartsWith ( "BINARY_PATH_NAME : " ) ) ;
75
45
76
- var line = lines
77
- . SingleOrDefault ( l => l . StartsWith ( "BINARY_PATH_NAME : " ) ) ;
46
+ if ( line == null )
47
+ {
48
+ Log . Debug ( "No existing binary path could be determined." ) ;
49
+ path = null ;
50
+ return false ;
51
+ }
78
52
79
- if ( line == null )
80
- {
81
- Console . WriteLine ( "No existing binary path could be determined." ) ;
82
- path = null ;
83
- return false ;
84
- }
53
+ path = line . Replace ( "BINARY_PATH_NAME : " , "" ) ;
54
+ return true ;
55
+ }
85
56
86
- path = line . Replace ( "BINARY_PATH_NAME : " , "" ) ;
57
+ public static bool GetServiceStoragePath ( string serviceName , out string ? storage )
58
+ {
59
+ if ( GetServiceCommandLine ( serviceName , out var binpath ) &&
60
+ binpath . Contains ( "--storage=\" " ) )
61
+ {
62
+ var start = binpath . IndexOf ( "--storage=\" " , StringComparison . Ordinal ) + 11 ;
63
+ var chop = binpath . Substring ( start ) ;
64
+ storage = chop . Substring ( 0 , chop . IndexOf ( '"' ) ) ;
87
65
return true ;
88
66
}
89
67
90
- public static bool GetServiceStoragePath ( string serviceName , out string ? storage )
91
- {
92
- if ( serviceName == null ) throw new ArgumentNullException ( nameof ( serviceName ) ) ;
93
-
94
- if ( GetServiceCommandLine ( serviceName , out var binpath ) &&
95
- binpath . Contains ( "--storage=\" " ) )
96
- {
97
- var start = binpath . IndexOf ( "--storage=\" " , StringComparison . Ordinal ) + 11 ;
98
- var chop = binpath . Substring ( start ) ;
99
- storage = chop . Substring ( 0 , chop . IndexOf ( '"' ) ) ;
100
- return true ;
101
- }
102
-
103
- storage = null ;
104
- return false ;
105
- }
68
+ storage = null ;
69
+ return false ;
106
70
}
107
- }
108
-
71
+ }
0 commit comments