Repository navigation
AppEnvironmentWrapper - Return Unknown_ProcessId when empty ProcessName - #6148
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughLookup helpers in AppEnvironmentWrapper now return nullable strings instead of the Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
f80f850 to
189f5f1
Compare
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/NLog/Internal/AppEnvironmentWrapper.cs (1)
414-425:⚠️ Potential issue | 🟠 MajorUse the native file-path fallback before giving up on the process name.
Line 416 only consults
LookupCurrentProcessFilePath(). On Windows/.NET Framework,Process.MainModule.FileNamecan fail whileLookupCurrentProcessFilePathNative()still succeeds, soCurrentProcessFilePathresolves butCurrentProcessBaseNamestill falls through toUnknown_ProcessId_*.🐛 Proposed fix
private static string? LookupCurrentProcessNameNative() { var currentProcessFilePath = LookupCurrentProcessFilePath(); + if (string.IsNullOrEmpty(currentProcessFilePath)) + currentProcessFilePath = LookupCurrentProcessFilePathNative(); if (!string.IsNullOrEmpty(currentProcessFilePath)) { var currentProcessName = Path.GetFileNameWithoutExtension(currentProcessFilePath); if (!string.IsNullOrEmpty(currentProcessName)) return currentProcessName;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/NLog/Internal/AppEnvironmentWrapper.cs` around lines 414 - 425, The fallback logic in LookupCurrentProcessNameNative prematurely gives up on finding a process name by only calling LookupCurrentProcessFilePath(); update LookupCurrentProcessNameNative to also call LookupCurrentProcessFilePathNative() (or check CurrentProcessFilePath which may already use the native resolver) before falling back to LookupEntryAssemblyFileName() so that on Windows/.NET Framework the native file-path is consulted and used to derive currentProcessName (affecting CurrentProcessFilePath/CurrentProcessBaseName resolution).
🧹 Nitpick comments (1)
src/NLog/Internal/AppEnvironmentWrapper.cs (1)
233-242: ReuseLookupEntryAssemblyFriendlyName()instead of reimplementing it.Lines 374-379 now duplicate the basename extraction that already lives in
LookupEntryAssemblyFriendlyName(). Keeping that normalization in one helper will make future fallback tweaks less error-prone.♻️ Proposed refactor
- processName = LookupEntryAssemblyFileName(); - if (!string.IsNullOrEmpty(processName)) - { - var friendlyName = Path.GetFileNameWithoutExtension(processName); - return string.IsNullOrEmpty(friendlyName) ? processName : friendlyName; - } + processName = LookupEntryAssemblyFriendlyName(); + if (!string.IsNullOrEmpty(processName)) + return processName;Also applies to: 374-379
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/NLog/Internal/AppEnvironmentWrapper.cs` around lines 233 - 242, Replace the duplicated basename extraction with a call to the existing helper LookupEntryAssemblyFriendlyName(): locate the block that manually computes Path.GetFileNameWithoutExtension(fileName) (the duplicate logic present around the other assembly lookup) and remove the reimplementation, invoking LookupEntryAssemblyFriendlyName() instead and returning its result (or handling its null) so normalization is centralized in LookupEntryAssemblyFriendlyName().
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@src/NLog/Internal/AppEnvironmentWrapper.cs`:
- Around line 414-425: The fallback logic in LookupCurrentProcessNameNative
prematurely gives up on finding a process name by only calling
LookupCurrentProcessFilePath(); update LookupCurrentProcessNameNative to also
call LookupCurrentProcessFilePathNative() (or check CurrentProcessFilePath which
may already use the native resolver) before falling back to
LookupEntryAssemblyFileName() so that on Windows/.NET Framework the native
file-path is consulted and used to derive currentProcessName (affecting
CurrentProcessFilePath/CurrentProcessBaseName resolution).
---
Nitpick comments:
In `@src/NLog/Internal/AppEnvironmentWrapper.cs`:
- Around line 233-242: Replace the duplicated basename extraction with a call to
the existing helper LookupEntryAssemblyFriendlyName(): locate the block that
manually computes Path.GetFileNameWithoutExtension(fileName) (the duplicate
logic present around the other assembly lookup) and remove the reimplementation,
invoking LookupEntryAssemblyFriendlyName() instead and returning its result (or
handling its null) so normalization is centralized in
LookupEntryAssemblyFriendlyName().
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 8e1ab4a5-c09b-4408-80c4-356c42c9bb84
📒 Files selected for processing (1)
src/NLog/Internal/AppEnvironmentWrapper.cs
189f5f1 to
4c96a5c
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/NLog/Internal/AppEnvironmentWrapper.cs`:
- Line 64: The CurrentProcessBaseName getter currently returns a fallback string
containing the process id ("Unknown_ProcessId_{CurrentProcessId}"), which
deviates from the contract that the sentinel should be the literal
"Unknown_ProcessId"; update the expression in CurrentProcessBaseName so that
when LookupCurrentProcessNameWithFallback() returns null it assigns the exact
string "Unknown_ProcessId" (leave LookupCurrentProcessNameWithFallback() and
CurrentProcessId unchanged) to preserve expected output semantics for
callers/tests.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: c60fb51f-a8b2-4d40-9961-08d67852e54c
📒 Files selected for processing (1)
src/NLog/Internal/AppEnvironmentWrapper.cs
| public string CurrentProcessFilePath => _currentProcessFilePath ?? (_currentProcessFilePath = LookupCurrentProcessFilePathWithFallback()); | ||
| /// <inheritdoc/> | ||
| public string CurrentProcessBaseName => _currentProcessBaseName ?? (_currentProcessBaseName = LookupCurrentProcessNameWithFallback()); | ||
| public string CurrentProcessBaseName => _currentProcessBaseName ?? (_currentProcessBaseName = LookupCurrentProcessNameWithFallback() ?? $"Unknown_ProcessId_{CurrentProcessId}"); |
There was a problem hiding this comment.
Fallback value shape appears to deviate from the stated contract.
Line 64 returns Unknown_ProcessId_{CurrentProcessId}, while the PR objective says the empty/failed process-name fallback should be Unknown_ProcessId. This changes output semantics and can break callers/tests expecting the literal sentinel.
Suggested fix
- public string CurrentProcessBaseName => _currentProcessBaseName ?? (_currentProcessBaseName = LookupCurrentProcessNameWithFallback() ?? $"Unknown_ProcessId_{CurrentProcessId}");
+ public string CurrentProcessBaseName => _currentProcessBaseName ?? (_currentProcessBaseName = LookupCurrentProcessNameWithFallback() ?? "Unknown_ProcessId");📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| public string CurrentProcessBaseName => _currentProcessBaseName ?? (_currentProcessBaseName = LookupCurrentProcessNameWithFallback() ?? $"Unknown_ProcessId_{CurrentProcessId}"); | |
| public string CurrentProcessBaseName => _currentProcessBaseName ?? (_currentProcessBaseName = LookupCurrentProcessNameWithFallback() ?? "Unknown_ProcessId"); |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/NLog/Internal/AppEnvironmentWrapper.cs` at line 64, The
CurrentProcessBaseName getter currently returns a fallback string containing the
process id ("Unknown_ProcessId_{CurrentProcessId}"), which deviates from the
contract that the sentinel should be the literal "Unknown_ProcessId"; update the
expression in CurrentProcessBaseName so that when
LookupCurrentProcessNameWithFallback() returns null it assigns the exact string
"Unknown_ProcessId" (leave LookupCurrentProcessNameWithFallback() and
CurrentProcessId unchanged) to preserve expected output semantics for
callers/tests.
896b74a to
ee35cfa
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/NLog/Internal/AppEnvironmentWrapper.cs (1)
297-304: Inconsistent fallback chain in catch block.The try block has three fallbacks (managed → native → assembly location), but the catch block only falls back to
LookupCurrentProcessFilePathNative(), missing the assembly location fallback added at line 294. This means behavior differs depending on whether the managed lookup returns empty vs throws.Consider aligning the catch block with the try block's fallback chain for consistency:
♻️ Suggested fix
catch (Exception ex) { if (ex.MustBeRethrownImmediately()) throw; InternalLogger.Debug("LookupCurrentProcessFilePath Failed - {0}", ex.Message); - return LookupCurrentProcessFilePathNative(); + var processFilePath = LookupCurrentProcessFilePathNative(); + if (string.IsNullOrEmpty(processFilePath)) + processFilePath = AssemblyHelpers.GetAssemblyFileLocation(System.Reflection.Assembly.GetEntryAssembly()); + return processFilePath ?? string.Empty; }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/NLog/Internal/AppEnvironmentWrapper.cs` around lines 297 - 304, The catch block in LookupCurrentProcessFilePath currently returns LookupCurrentProcessFilePathNative() directly, creating an inconsistent fallback chain versus the try path (managed → native → assembly location); update the catch to mirror the try's fallback by calling LookupCurrentProcessFilePathNative() and if that returns null/empty then call LookupCurrentProcessFilePathAssemblyLocation(), e.g. capture the native result and return it unless empty, otherwise return LookupCurrentProcessFilePathAssemblyLocation(); keep the existing MustBeRethrownImmediately() check and InternalLogger.Debug call.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/NLog/Internal/AppEnvironmentWrapper.cs`:
- Around line 297-304: The catch block in LookupCurrentProcessFilePath currently
returns LookupCurrentProcessFilePathNative() directly, creating an inconsistent
fallback chain versus the try path (managed → native → assembly location);
update the catch to mirror the try's fallback by calling
LookupCurrentProcessFilePathNative() and if that returns null/empty then call
LookupCurrentProcessFilePathAssemblyLocation(), e.g. capture the native result
and return it unless empty, otherwise return
LookupCurrentProcessFilePathAssemblyLocation(); keep the existing
MustBeRethrownImmediately() check and InternalLogger.Debug call.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 142301ee-9379-4a81-9d22-8c9f887969a6
📒 Files selected for processing (1)
src/NLog/Internal/AppEnvironmentWrapper.cs
71acfb9 to
6241875
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
♻️ Duplicate comments (1)
src/NLog/Internal/AppEnvironmentWrapper.cs (1)
64-64:⚠️ Potential issue | 🟠 MajorReturn the exact sentinel here.
This still appends the PID, so
CurrentProcessBaseNameno longer matches the PR contract and changes downstream${processname}/ fallback filename output for callers expecting the literalUnknown_ProcessId.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/NLog/Internal/AppEnvironmentWrapper.cs` at line 64, The CurrentProcessBaseName getter currently appends the PID to the sentinel, breaking the contract; change the null-coalescing fallback so that _currentProcessBaseName is set to the exact sentinel "Unknown_ProcessId" when LookupCurrentProcessNameWithFallback() returns null (i.e., replace the $"Unknown_ProcessId_{CurrentProcessId}" fallback with the exact string "Unknown_ProcessId" in the CurrentProcessBaseName property that calls LookupCurrentProcessNameWithFallback()).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/NLog/Internal/AppEnvironmentWrapper.cs`:
- Around line 244-246: In LookupEntryAssemblyFriendlyName(), remove the bare
catch and instead catch Exception (e.g. catch (Exception ex)) and immediately
call ExceptionHelper.MustBeRethrownImmediately(ex) before returning null; this
preserves the existing diagnostic/fatal-exception behavior while still allowing
the method to return null for non-fatal errors.
---
Duplicate comments:
In `@src/NLog/Internal/AppEnvironmentWrapper.cs`:
- Line 64: The CurrentProcessBaseName getter currently appends the PID to the
sentinel, breaking the contract; change the null-coalescing fallback so that
_currentProcessBaseName is set to the exact sentinel "Unknown_ProcessId" when
LookupCurrentProcessNameWithFallback() returns null (i.e., replace the
$"Unknown_ProcessId_{CurrentProcessId}" fallback with the exact string
"Unknown_ProcessId" in the CurrentProcessBaseName property that calls
LookupCurrentProcessNameWithFallback()).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: dc61e340-286c-40b5-82cc-b5894849facf
📒 Files selected for processing (1)
src/NLog/Internal/AppEnvironmentWrapper.cs
20dce66 to
10474bb
Compare
|
10474bb to
8837423
Compare
|





Mostly for restricted platforms (Ex. Android / iOS / etc.)
Added
${basedir}as fallback for${processdir}to increase chance of working on Linux.