/// <summary> /// Adds the console, debugger, file, or host listener if requested. /// </summary> internal void AddTraceListenersToSources(Collection <PSTraceSource> matchingSources) { if (DebuggerListener) { if (_defaultListener == null) { _defaultListener = new DefaultTraceListener(); // Note, this is not meant to be localized. _defaultListener.Name = "Debug"; } AddListenerToSources(matchingSources, _defaultListener); } if (PSHostListener) { if (_hostListener == null) { ((MshCommandRuntime)this.CommandRuntime).DebugPreference = ActionPreference.Continue; _hostListener = new PSHostTraceListener(this); // Note, this is not meant to be localized. _hostListener.Name = "Host"; } AddListenerToSources(matchingSources, _hostListener); } if (FileListener != null) { if (_fileListeners == null) { _fileListeners = new Collection <TextWriterTraceListener>(); FileStreams = new Collection <FileStream>(); Exception error = null; try { Collection <string> resolvedPaths = new Collection <string>(); try { // Resolve the file path ProviderInfo provider = null; resolvedPaths = this.SessionState.Path.GetResolvedProviderPathFromPSPath(FileListener, out provider); // We can only export aliases to the file system if (!provider.NameEquals(this.Context.ProviderNames.FileSystem)) { throw new PSNotSupportedException( StringUtil.Format(TraceCommandStrings.TraceFileOnly, FileListener, provider.FullName)); } } catch (ItemNotFoundException) { // Since the file wasn't found, just make a provider-qualified path out if it // and use that. PSDriveInfo driveInfo = null; ProviderInfo provider = null; string path = this.SessionState.Path.GetUnresolvedProviderPathFromPSPath( FileListener, new CmdletProviderContext(this.Context), out provider, out driveInfo); // We can only export aliases to the file system if (!provider.NameEquals(this.Context.ProviderNames.FileSystem)) { throw new PSNotSupportedException( StringUtil.Format(TraceCommandStrings.TraceFileOnly, FileListener, provider.FullName)); } resolvedPaths.Add(path); } if (resolvedPaths.Count > 1) { throw new PSNotSupportedException(StringUtil.Format(TraceCommandStrings.TraceSingleFileOnly, FileListener)); } string resolvedPath = resolvedPaths[0]; Exception fileOpenError = null; try { if (ForceWrite && System.IO.File.Exists(resolvedPath)) { // remove readonly attributes on the file System.IO.FileInfo fInfo = new System.IO.FileInfo(resolvedPath); if (fInfo != null) { // Save some disk write time by checking whether file is readonly.. if ((fInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { // Make sure the file is not read only fInfo.Attributes &= ~(FileAttributes.ReadOnly); } } } // Trace commands always append..So there is no need to set overwrite with force.. FileStream fileStream = new FileStream(resolvedPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); FileStreams.Add(fileStream); // Open the file stream TextWriterTraceListener fileListener = new TextWriterTraceListener(fileStream, resolvedPath); fileListener.Name = FileListener; _fileListeners.Add(fileListener); } catch (IOException ioException) { fileOpenError = ioException; } catch (SecurityException securityException) { fileOpenError = securityException; } catch (UnauthorizedAccessException unauthorized) { fileOpenError = unauthorized; } if (fileOpenError != null) { ErrorRecord errorRecord = new ErrorRecord( fileOpenError, "FileListenerPathResolutionFailed", ErrorCategory.OpenError, resolvedPath); WriteError(errorRecord); } } catch (ProviderNotFoundException providerNotFound) { error = providerNotFound; } catch (System.Management.Automation.DriveNotFoundException driveNotFound) { error = driveNotFound; } catch (NotSupportedException notSupported) { error = notSupported; } if (error != null) { ErrorRecord errorRecord = new ErrorRecord( error, "FileListenerPathResolutionFailed", ErrorCategory.InvalidArgument, FileListener); WriteError(errorRecord); } } foreach (TraceListener listener in _fileListeners) { AddListenerToSources(matchingSources, listener); } } }
/// <summary> /// Adds the console, debugger, file, or host listener /// if requested. /// </summary> /// internal void AddTraceListenersToSources(Collection<PSTraceSource> matchingSources) { if (DebuggerListener) { if (_defaultListener == null) { _defaultListener = new DefaultTraceListener(); // Note, this is not meant to be localized. _defaultListener.Name = "Debug"; } AddListenerToSources(matchingSources, _defaultListener); } if (PSHostListener) { if (_hostListener == null) { ((MshCommandRuntime)this.CommandRuntime).DebugPreference = ActionPreference.Continue; _hostListener = new PSHostTraceListener(this); // Note, this is not meant to be localized. _hostListener.Name = "Host"; } AddListenerToSources(matchingSources, _hostListener); } if (FileListener != null) { if (_fileListeners == null) { _fileListeners = new Collection<TextWriterTraceListener>(); FileStreams = new Collection<FileStream>(); Exception error = null; try { Collection<string> resolvedPaths = new Collection<string>(); try { // Resolve the file path ProviderInfo provider = null; resolvedPaths = this.SessionState.Path.GetResolvedProviderPathFromPSPath(FileListener, out provider); // We can only export aliases to the file system if (!provider.NameEquals(this.Context.ProviderNames.FileSystem)) { throw new PSNotSupportedException( StringUtil.Format(TraceCommandStrings.TraceFileOnly, FileListener, provider.FullName)); } } catch (ItemNotFoundException) { // Since the file wasn't found, just make a provider-qualified path out if it // and use that. PSDriveInfo driveInfo = null; ProviderInfo provider = null; string path = this.SessionState.Path.GetUnresolvedProviderPathFromPSPath( FileListener, new CmdletProviderContext(this.Context), out provider, out driveInfo); // We can only export aliases to the file system if (!provider.NameEquals(this.Context.ProviderNames.FileSystem)) { throw new PSNotSupportedException( StringUtil.Format(TraceCommandStrings.TraceFileOnly, FileListener, provider.FullName)); } resolvedPaths.Add(path); } if (resolvedPaths.Count > 1) { throw new PSNotSupportedException(StringUtil.Format(TraceCommandStrings.TraceSingleFileOnly, FileListener)); } string resolvedPath = resolvedPaths[0]; Exception fileOpenError = null; try { if (ForceWrite && System.IO.File.Exists(resolvedPath)) { // remove readonly attributes on the file System.IO.FileInfo fInfo = new System.IO.FileInfo(resolvedPath); if (fInfo != null) { // Save some disk write time by checking whether file is readonly.. if ((fInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { //Make sure the file is not read only fInfo.Attributes &= ~(FileAttributes.ReadOnly); } } } // Trace commands always append..So there is no need to set overwrite with force.. FileStream fileStream = new FileStream(resolvedPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); FileStreams.Add(fileStream); // Open the file stream TextWriterTraceListener fileListener = new TextWriterTraceListener(fileStream, resolvedPath); fileListener.Name = FileListener; _fileListeners.Add(fileListener); } catch (IOException ioException) { fileOpenError = ioException; } catch (SecurityException securityException) { fileOpenError = securityException; } catch (UnauthorizedAccessException unauthorized) { fileOpenError = unauthorized; } if (fileOpenError != null) { ErrorRecord errorRecord = new ErrorRecord( fileOpenError, "FileListenerPathResolutionFailed", ErrorCategory.OpenError, resolvedPath); WriteError(errorRecord); } } catch (ProviderNotFoundException providerNotFound) { error = providerNotFound; } catch (System.Management.Automation.DriveNotFoundException driveNotFound) { error = driveNotFound; } catch (NotSupportedException notSupported) { error = notSupported; } if (error != null) { ErrorRecord errorRecord = new ErrorRecord( error, "FileListenerPathResolutionFailed", ErrorCategory.InvalidArgument, FileListener); WriteError(errorRecord); } } foreach (TraceListener listener in _fileListeners) { AddListenerToSources(matchingSources, listener); } } }
internal void AddTraceListenersToSources(Collection<PSTraceSource> matchingSources) { if (this.DebuggerListener) { if (this.defaultListener == null) { this.defaultListener = new DefaultTraceListener(); this.defaultListener.Name = "Debug"; } AddListenerToSources(matchingSources, this.defaultListener); } if (this.PSHostListener != 0) { if (this.hostListener == null) { ((MshCommandRuntime) base.CommandRuntime).DebugPreference = ActionPreference.Continue; this.hostListener = new PSHostTraceListener(this); this.hostListener.Name = "Host"; } AddListenerToSources(matchingSources, this.hostListener); } if (this.FileListener != null) { if (this.fileListeners == null) { this.fileListeners = new Collection<TextWriterTraceListener>(); this.fileStreams = new Collection<FileStream>(); Exception exception = null; try { Collection<string> resolvedProviderPathFromPSPath = new Collection<string>(); try { ProviderInfo provider = null; resolvedProviderPathFromPSPath = base.SessionState.Path.GetResolvedProviderPathFromPSPath(this.file, out provider); if (!provider.NameEquals(base.Context.ProviderNames.FileSystem)) { throw new PSNotSupportedException(StringUtil.Format(TraceCommandStrings.TraceFileOnly, this.file, provider.FullName)); } } catch (ItemNotFoundException) { PSDriveInfo drive = null; ProviderInfo info3 = null; string item = base.SessionState.Path.GetUnresolvedProviderPathFromPSPath(this.file, new CmdletProviderContext(base.Context), out info3, out drive); if (!info3.NameEquals(base.Context.ProviderNames.FileSystem)) { throw new PSNotSupportedException(StringUtil.Format(TraceCommandStrings.TraceFileOnly, this.file, info3.FullName)); } resolvedProviderPathFromPSPath.Add(item); } if (resolvedProviderPathFromPSPath.Count > 1) { throw new PSNotSupportedException(StringUtil.Format(TraceCommandStrings.TraceSingleFileOnly, this.file)); } string path = resolvedProviderPathFromPSPath[0]; Exception exception2 = null; try { if (this.ForceWrite && File.Exists(path)) { FileInfo info4 = new FileInfo(path); if ((info4 != null) && ((info4.Attributes & System.IO.FileAttributes.ReadOnly) == System.IO.FileAttributes.ReadOnly)) { info4.Attributes &= ~System.IO.FileAttributes.ReadOnly; } } FileStream stream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); this.fileStreams.Add(stream); TextWriterTraceListener listener = new TextWriterTraceListener(stream, path) { Name = this.file }; this.fileListeners.Add(listener); } catch (IOException exception3) { exception2 = exception3; } catch (SecurityException exception4) { exception2 = exception4; } catch (UnauthorizedAccessException exception5) { exception2 = exception5; } if (exception2 != null) { ErrorRecord errorRecord = new ErrorRecord(exception2, "FileListenerPathResolutionFailed", ErrorCategory.OpenError, path); base.WriteError(errorRecord); } } catch (ProviderNotFoundException exception6) { exception = exception6; } catch (System.Management.Automation.DriveNotFoundException exception7) { exception = exception7; } catch (NotSupportedException exception8) { exception = exception8; } if (exception != null) { ErrorRecord record2 = new ErrorRecord(exception, "FileListenerPathResolutionFailed", ErrorCategory.InvalidArgument, this.file); base.WriteError(record2); } } foreach (TraceListener listener2 in this.fileListeners) { AddListenerToSources(matchingSources, listener2); } } }
internal void AddTraceListenersToSources(Collection <PSTraceSource> matchingSources) { if (this.DebuggerListener) { if (this.defaultListener == null) { this.defaultListener = new DefaultTraceListener(); this.defaultListener.Name = "Debug"; } AddListenerToSources(matchingSources, this.defaultListener); } if (this.PSHostListener != 0) { if (this.hostListener == null) { ((MshCommandRuntime)base.CommandRuntime).DebugPreference = ActionPreference.Continue; this.hostListener = new PSHostTraceListener(this); this.hostListener.Name = "Host"; } AddListenerToSources(matchingSources, this.hostListener); } if (this.FileListener != null) { if (this.fileListeners == null) { this.fileListeners = new Collection <TextWriterTraceListener>(); this.fileStreams = new Collection <FileStream>(); Exception exception = null; try { Collection <string> resolvedProviderPathFromPSPath = new Collection <string>(); try { ProviderInfo provider = null; resolvedProviderPathFromPSPath = base.SessionState.Path.GetResolvedProviderPathFromPSPath(this.file, out provider); if (!provider.NameEquals(base.Context.ProviderNames.FileSystem)) { throw new PSNotSupportedException(StringUtil.Format(TraceCommandStrings.TraceFileOnly, this.file, provider.FullName)); } } catch (ItemNotFoundException) { PSDriveInfo drive = null; ProviderInfo info3 = null; string item = base.SessionState.Path.GetUnresolvedProviderPathFromPSPath(this.file, new CmdletProviderContext(base.Context), out info3, out drive); if (!info3.NameEquals(base.Context.ProviderNames.FileSystem)) { throw new PSNotSupportedException(StringUtil.Format(TraceCommandStrings.TraceFileOnly, this.file, info3.FullName)); } resolvedProviderPathFromPSPath.Add(item); } if (resolvedProviderPathFromPSPath.Count > 1) { throw new PSNotSupportedException(StringUtil.Format(TraceCommandStrings.TraceSingleFileOnly, this.file)); } string path = resolvedProviderPathFromPSPath[0]; Exception exception2 = null; try { if (this.ForceWrite && File.Exists(path)) { FileInfo info4 = new FileInfo(path); if ((info4 != null) && ((info4.Attributes & System.IO.FileAttributes.ReadOnly) == System.IO.FileAttributes.ReadOnly)) { info4.Attributes &= ~System.IO.FileAttributes.ReadOnly; } } FileStream stream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); this.fileStreams.Add(stream); TextWriterTraceListener listener = new TextWriterTraceListener(stream, path) { Name = this.file }; this.fileListeners.Add(listener); } catch (IOException exception3) { exception2 = exception3; } catch (SecurityException exception4) { exception2 = exception4; } catch (UnauthorizedAccessException exception5) { exception2 = exception5; } if (exception2 != null) { ErrorRecord errorRecord = new ErrorRecord(exception2, "FileListenerPathResolutionFailed", ErrorCategory.OpenError, path); base.WriteError(errorRecord); } } catch (ProviderNotFoundException exception6) { exception = exception6; } catch (System.Management.Automation.DriveNotFoundException exception7) { exception = exception7; } catch (NotSupportedException exception8) { exception = exception8; } if (exception != null) { ErrorRecord record2 = new ErrorRecord(exception, "FileListenerPathResolutionFailed", ErrorCategory.InvalidArgument, this.file); base.WriteError(record2); } } foreach (TraceListener listener2 in this.fileListeners) { AddListenerToSources(matchingSources, listener2); } } }