@@ -15,15 +15,19 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1515You should have received a copy of the GNU General Public License
1616along with this program. If not, see <https://www.gnu.org/licenses/>.
1717*/
18+ using Microsoft . Extensions . Logging ;
1819using Microsoft . Xrm . Sdk ;
1920using System ;
21+ using System . Collections . Concurrent ;
2022using System . Management . Automation ;
2123using System . ServiceModel ;
2224
2325namespace AMSoftware . Dataverse . PowerShell
2426{
2527 public abstract class CmdletBase : PSCmdlet
2628 {
29+ private ConcurrentQueue < PSLogEntry > LogMessages { get ; set ; } = new ConcurrentQueue < PSLogEntry > ( ) ;
30+
2731 public virtual void Execute ( )
2832 {
2933 // Do nothing
@@ -39,6 +43,16 @@ protected override void BeginProcessing()
3943 {
4044 WriteVerboseWithTimestamp ( string . Format ( "{0} begin processing with ParameterSet '{1}'." , GetType ( ) . Name , ParameterSetName ) ) ;
4145 }
46+
47+ MSALIdentityLogger . Instance . AddContext ( ( entry ) =>
48+ {
49+ LogMessages . Enqueue ( entry ) ;
50+ } ) ;
51+ ServiceClientLogger . Instance . AddContext ( ( entry ) =>
52+ {
53+ LogMessages . Enqueue ( entry ) ;
54+ } ) ;
55+
4256 base . BeginProcessing ( ) ;
4357 }
4458
@@ -69,23 +83,101 @@ protected override void ProcessRecord()
6983
7084 protected override void EndProcessing ( )
7185 {
86+ MSALIdentityLogger . Instance . RemoveContext ( ) ;
87+ ServiceClientLogger . Instance . RemoveContext ( ) ;
88+
89+ FlushLogMessages ( ) ;
90+
7291 WriteVerboseWithTimestamp ( string . Format ( "{0} end processing." , GetType ( ) . Name ) ) ;
7392
7493 base . EndProcessing ( ) ;
7594 }
7695
77- protected new void WriteObject ( object sendToPipeline )
96+ private void FlushLogMessages ( )
7897 {
79- //FlushDebugMessages();
80- base . WriteObject ( sendToPipeline ) ;
98+ while ( LogMessages . TryDequeue ( out PSLogEntry entry ) )
99+ {
100+ switch ( entry . LogLevel )
101+ {
102+ case LogLevel . Warning :
103+ base . WriteWarning ( entry . Message ) ;
104+ break ;
105+ case LogLevel . Debug :
106+ base . WriteDebug ( entry . Message ) ;
107+ break ;
108+ case LogLevel . Trace :
109+ base . WriteVerbose ( entry . Message ) ;
110+ break ;
111+ }
112+ }
113+ }
114+
115+ protected new void ThrowTerminatingError ( ErrorRecord errorRecord )
116+ {
117+ FlushLogMessages ( ) ;
118+ base . ThrowTerminatingError ( errorRecord ) ;
119+ }
120+
121+ protected new void WriteCommandDetail ( string text )
122+ {
123+ FlushLogMessages ( ) ;
124+ base . WriteCommandDetail ( text ) ;
125+ }
126+
127+ protected new void WriteDebug ( string text )
128+ {
129+ FlushLogMessages ( ) ;
130+ base . WriteDebug ( text ) ;
131+ }
132+
133+ protected new void WriteError ( ErrorRecord errorRecord )
134+ {
135+ FlushLogMessages ( ) ;
136+ base . WriteError ( errorRecord ) ;
137+ }
138+
139+ protected new void WriteInformation ( object messageData , string [ ] tags )
140+ {
141+ FlushLogMessages ( ) ;
142+ base . WriteInformation ( messageData , tags ) ;
143+ }
144+
145+ protected new void WriteInformation ( InformationRecord informationRecord )
146+ {
147+ FlushLogMessages ( ) ;
148+ base . WriteInformation ( informationRecord ) ;
81149 }
82150
83151 protected new void WriteObject ( object sendToPipeline , bool enumerateCollection )
84152 {
85- //FlushDebugMessages ();
153+ FlushLogMessages ( ) ;
86154 base . WriteObject ( sendToPipeline , enumerateCollection ) ;
87155 }
88156
157+ protected new void WriteObject ( object sendToPipeline )
158+ {
159+ FlushLogMessages ( ) ;
160+ base . WriteObject ( sendToPipeline ) ;
161+ }
162+
163+ protected new void WriteProgress ( ProgressRecord progressRecord )
164+ {
165+ FlushLogMessages ( ) ;
166+ base . WriteProgress ( progressRecord ) ;
167+ }
168+
169+ protected new void WriteVerbose ( string text )
170+ {
171+ FlushLogMessages ( ) ;
172+ base . WriteVerbose ( text ) ;
173+ }
174+
175+ protected new void WriteWarning ( string text )
176+ {
177+ FlushLogMessages ( ) ;
178+ base . WriteWarning ( text ) ;
179+ }
180+
89181 protected bool IsTerminatingError ( Exception ex )
90182 {
91183 var pipelineStoppedEx = ex as PipelineStoppedException ;
0 commit comments