A sink for PowerShell `Write-Debug`, `Write-Verbose`, etc.?
See original GitHub issueA few questions before you begin:
Does this issue relate to a new feature or an existing bug?
- Bug
- [x ] New Feature
What version of Serilog is affected? Please list the related NuGet package.
All
**What is the target framework and operating system? See [target frameworks]
- [x ] netCore 3.1
- netCore 2.0
- netCore 1.0
- 4.7
- 4.6.x
- 4.5.x
Please describe the current behavior?
I can’t find a Serilog sink that I can use from a PowerShell CmdLet that I’m building in C#. The CmdLet utilizes a library I’ve built that makes extensive use of Serilog for Debug
and Information
.
I currently setup Serilog like this:
// Setup logging
Log.Logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(MasterLevelSwitch)
.WriteTo.Console(levelSwitch: ConsoleLevelSwitch)
.WriteTo.Debug(levelSwitch: DebugLevelSwitch)
.WriteTo.File(LogPath, shared: true, levelSwitch: FileLevelSwitch)
.CreateLogger();
I want to be able to add to my configuration, when my library is used from PowerShell:
// Setup logging
Log.Logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(MasterLevelSwitch)
.WriteTo.PowerShellWriteVerbose(levelSwitch: ConsoleLevelSwitch)
.WriteTo.PowerShellWriteDebug(levelSwitch: DebugLevelSwitch)
.WriteTo.File(LogPath, shared: true, levelSwitch: FileLevelSwitch)
.CreateLogger();
(roughly).
I’ve searched for a Serilog sink for PowerShell’s Write-Output
, Write-Verbose
, Write-Debug
, etc… to no avail. I suppose it wouldn’t be hard to write one, but I can’t believe I’m the first person to think of this…
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
@tig Would it be safer to instantiate the
StringWriter strWriter = new StringWriter();
inside the _syncRoot lock? Otherwise, other threads might change thestrWriter
variable before the writer gets a chance to write out the message.@tig thanks for your PowershellSink example. I found that the (text) output capability of Cmdlet was limiting - especially when you want to simply write to stderr - but AsyncCmdlet derives from a PSCmdlet which has a Host property, which in turn has a UI property of type PSHostUserInterface and this is much more useful: