Skip to content

Commit 89b66a9

Browse files
committed
Separate queue and log folders per Windows Service Instance
See issues #357 and #355
1 parent e1857e4 commit 89b66a9

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

src/ServiceControl/Bootstrapper.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public class Bootstrapper
2222

2323
public Bootstrapper(ServiceBase host = null)
2424
{
25+
Settings.ServiceName = "foo"; // DetermineServiceName(host);
2526
ConfigureLogging();
26-
2727
var containerBuilder = new ContainerBuilder();
28-
28+
2929
Container = containerBuilder.Build();
3030

3131
// Disable Auditing for the service control endpoint
@@ -39,11 +39,10 @@ public Bootstrapper(ServiceBase host = null)
3939

4040
Feature.EnableByDefault<StorageDrivenPublisher>();
4141
Configure.ScaleOut(s => s.UseSingleBrokerQueue());
42-
4342
var transportType = Type.GetType(Settings.TransportType);
4443
bus = Configure
4544
.With(AllAssemblies.Except("ServiceControl.Plugin"))
46-
.DefineEndpointName("Particular.ServiceControl")
45+
.DefineEndpointName(Settings.ServiceName)
4746
.AutofacBuilder(Container)
4847
.UseTransport(transportType)
4948
.MessageForwardingInCaseOfFault()
@@ -83,7 +82,7 @@ static void ConfigureLogging()
8382

8483
var nlogConfig = new LoggingConfiguration();
8584
var simpleLayout = new SimpleLayout("${longdate}|${threadid}|${level}|${logger}|${message}${onexception:${newline}${exception:format=tostring}}");
86-
85+
8786
var fileTarget = new FileTarget
8887
{
8988
ArchiveEvery = FileArchivePeriod.Day,
@@ -112,5 +111,14 @@ static void ConfigureLogging()
112111
NLogConfigurator.Configure(new object[] { fileTarget, consoleTarget }, "Info");
113112
LogManager.Configuration = nlogConfig;
114113
}
114+
115+
string DetermineServiceName(ServiceBase host)
116+
{
117+
if ((host == null) || (string.IsNullOrWhiteSpace(host.ServiceName)))
118+
{
119+
return "Particular.ServiceControl";
120+
}
121+
return host.ServiceName;
122+
}
115123
}
116124
}

src/ServiceControl/ConfigErrorQueue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public MessageForwardingInCaseOfFaultConfig GetConfiguration()
99
{
1010
return new MessageForwardingInCaseOfFaultConfig
1111
{
12-
ErrorQueue = "Particular.ServiceControl.Errors"
12+
ErrorQueue = string.Format("{0}.Errors", NServiceBus.Configure.EndpointName)
1313
};
1414
}
1515
}

src/ServiceControl/Infrastructure/Settings/Settings.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,13 @@ static string SanitiseFolderName(string folderName)
134134
public static TimeSpan HeartbeatGracePeriod = TimeSpan.Parse(SettingsReader<string>.Read("HeartbeatGracePeriod", "00:00:40"));
135135
public static string TransportType { get; set; }
136136

137-
public static readonly string LogPath =
138-
Environment.ExpandEnvironmentVariables(SettingsReader<string>.Read("LogPath",
139-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
140-
"Particular\\ServiceControl\\logs")));
137+
public static string LogPath
138+
{
139+
get
140+
{
141+
return Environment.ExpandEnvironmentVariables(SettingsReader<string>.Read("LogPath", DefaultLogPathForInstance()));
142+
}
143+
}
141144

142145
public static string DbPath;
143146
public static Address ErrorLogQueue;
@@ -151,5 +154,17 @@ static string SanitiseFolderName(string folderName)
151154
public static int HoursToKeepMessagesBeforeExpiring = SettingsReader<int>.Read("HoursToKeepMessagesBeforeExpiring", 24 * 30); // default is 30 days
152155

153156
static readonly ILog Logger = LogManager.GetLogger(typeof(Settings));
157+
public static string ServiceName;
158+
159+
static string DefaultLogPathForInstance()
160+
{
161+
if (ServiceName.Equals("Particular.ServiceControl", StringComparison.OrdinalIgnoreCase))
162+
{
163+
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Particular\\ServiceControl\\logs");
164+
}
165+
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
166+
string.Format("Particular\\ServiceControl-{0}\\logs", ServiceName));
167+
168+
}
154169
}
155170
}

0 commit comments

Comments
 (0)