Skip to content

Commit f1df4f0

Browse files
authored
Avoid creating RpcLogger instances unnecessarily by adding static method 'WriteSystemLog' to RpcLogger (#170)
1 parent 2f757de commit f1df4f0

File tree

6 files changed

+39
-30
lines changed

6 files changed

+39
-30
lines changed

src/Logging/RpcLogger.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,34 @@ public void Log(LogLevel logLevel, string message, Exception exception = null, b
5959
}
6060
else
6161
{
62-
// For system logs, we log to stdio with a prefix of LanguageWorkerConsoleLog.
63-
// These are picked up by the Functions Host
64-
_systemLogMsg.Append(SystemLogPrefix).AppendLine("System Log: {");
65-
if (!string.IsNullOrEmpty(_requestId))
66-
{
67-
_systemLogMsg.AppendLine($" Request-Id: {_requestId}");
68-
}
69-
if (!string.IsNullOrEmpty(_invocationId))
70-
{
71-
_systemLogMsg.AppendLine($" Invocation-Id: {_invocationId}");
72-
}
73-
_systemLogMsg.AppendLine($" Log-Message: {message}").AppendLine("}");
62+
WriteSystemLog(message, _systemLogMsg, _requestId, _invocationId);
63+
}
64+
}
7465

75-
Console.WriteLine(_systemLogMsg.ToString());
76-
_systemLogMsg.Clear();
66+
private static void WriteSystemLog(string message, StringBuilder stringBuilder, string requestId, string invocationId)
67+
{
68+
stringBuilder = stringBuilder ?? new StringBuilder();
69+
70+
// For system logs, we log to stdio with a prefix of LanguageWorkerConsoleLog.
71+
// These are picked up by the Functions Host
72+
stringBuilder.Append(SystemLogPrefix).AppendLine("System Log: {");
73+
if (!string.IsNullOrEmpty(requestId))
74+
{
75+
stringBuilder.AppendLine($" Request-Id: {requestId}");
7776
}
77+
if (!string.IsNullOrEmpty(invocationId))
78+
{
79+
stringBuilder.AppendLine($" Invocation-Id: {invocationId}");
80+
}
81+
stringBuilder.AppendLine($" Log-Message: {message}").AppendLine("}");
82+
83+
Console.WriteLine(stringBuilder.ToString());
84+
stringBuilder.Clear();
85+
}
86+
87+
internal static void WriteSystemLog(string message)
88+
{
89+
WriteSystemLog(message, stringBuilder: null, requestId: null, invocationId: null);
7890
}
7991
}
8092
}

src/PowerShell/PowerShellManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ internal void InvokeProfile(string profilePath)
9292
Exception exception = null;
9393
if (profilePath == null)
9494
{
95-
_logger.Log(LogLevel.Trace, string.Format(PowerShellWorkerStrings.FileNotFound, "profile.ps1", FunctionLoader.FunctionAppRootPath));
95+
RpcLogger.WriteSystemLog(string.Format(PowerShellWorkerStrings.FileNotFound, "profile.ps1", FunctionLoader.FunctionAppRootPath));
9696
return;
9797
}
9898

src/PowerShell/PowerShellManagerPool.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ internal class PowerShellManagerPool
3131
internal PowerShellManagerPool(MessagingStream msgStream)
3232
{
3333
string upperBound = Environment.GetEnvironmentVariable("PSWorkerInProcConcurrencyUpperBound");
34+
RpcLogger.WriteSystemLog(string.Format(PowerShellWorkerStrings.LogConcurrencyUpperBound, upperBound));
35+
3436
if (string.IsNullOrEmpty(upperBound) || !int.TryParse(upperBound, out _upperBound))
3537
{
3638
_upperBound = 1;
@@ -81,6 +83,8 @@ internal PowerShellManager CheckoutIdleWorker(StreamingMessage request, AzFuncti
8183
var logger = new RpcLogger(_msgStream);
8284
logger.SetContext(requestId, invocationId);
8385
psManager = new PowerShellManager(logger);
86+
87+
RpcLogger.WriteSystemLog(string.Format(PowerShellWorkerStrings.LogNewPowerShellManagerCreated, _poolSize));
8488
}
8589
else
8690
{

src/RequestProcessor.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ namespace Microsoft.Azure.Functions.PowerShellWorker
2121
internal class RequestProcessor
2222
{
2323
private readonly FunctionLoader _functionLoader;
24-
private readonly RpcLogger _logger;
2524
private readonly MessagingStream _msgStream;
2625
private readonly PowerShellManagerPool _powershellPool;
2726
private readonly DependencyManager _dependencyManager;
@@ -39,7 +38,6 @@ internal class RequestProcessor
3938
internal RequestProcessor(MessagingStream msgStream)
4039
{
4140
_msgStream = msgStream;
42-
_logger = new RpcLogger(_msgStream);
4341
_powershellPool = new PowerShellManagerPool(msgStream);
4442
_functionLoader = new FunctionLoader();
4543
_dependencyManager = new DependencyManager();
@@ -83,7 +81,7 @@ internal async Task ProcessRequestLoop()
8381
}
8482
else
8583
{
86-
_logger.Log(LogLevel.Error, string.Format(PowerShellWorkerStrings.UnsupportedMessage, request.ContentCase));
84+
RpcLogger.WriteSystemLog(string.Format(PowerShellWorkerStrings.UnsupportedMessage, request.ContentCase));
8785
continue;
8886
}
8987

@@ -107,7 +105,7 @@ internal StreamingMessage ProcessWorkerInitRequest(StreamingMessage request)
107105
string pipeName = Environment.GetEnvironmentVariable("PSWorkerCustomPipeName");
108106
if (!string.IsNullOrEmpty(pipeName))
109107
{
110-
_logger.Log(LogLevel.Information, string.Format(PowerShellWorkerStrings.SpecifiedCustomPipeName, pipeName));
108+
RpcLogger.WriteSystemLog(string.Format(PowerShellWorkerStrings.SpecifiedCustomPipeName, pipeName));
111109
RemoteSessionNamedPipeServer.CreateCustomNamedPipeServer(pipeName);
112110
}
113111

src/resources/PowerShellWorkerStrings.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,10 @@
205205
<data name="FailToClenupModuleDestinationPath" xml:space="preserve">
206206
<value>Failed to clean up module destination path '{0}'</value>
207207
</data>
208+
<data name="LogConcurrencyUpperBound" xml:space="preserve">
209+
<value>The enforced concurrency level (pool size limit) is '{0}'.</value>
210+
</data>
211+
<data name="LogNewPowerShellManagerCreated" xml:space="preserve">
212+
<value>A new PowerShell manager instance is added to the pool. Current pool size '{0}'.</value>
213+
</data>
208214
</root>

test/Unit/PowerShell/PowerShellManagerTests.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,6 @@ public void ProfileShouldWork()
177177
Assert.Equal("Information: INFORMATION: Hello PROFILE", _testLogger.FullLog[0]);
178178
}
179179

180-
[Fact]
181-
public void ProfileDoesNotExist()
182-
{
183-
//initialize fresh log
184-
_testLogger.FullLog.Clear();
185-
_testManager.InvokeProfile(null);
186-
187-
Assert.Single(_testLogger.FullLog);
188-
Assert.Matches("Trace: No 'profile.ps1' is found at the FunctionApp root folder: ", _testLogger.FullLog[0]);
189-
}
190-
191180
[Fact]
192181
public void ProfileWithTerminatingError()
193182
{

0 commit comments

Comments
 (0)