@@ -80,6 +80,21 @@ protected TraceListenerBase(string name)
8080
8181 #endregion
8282
83+ /// <summary>
84+ /// Gets the process identifier.
85+ /// </summary>
86+ protected int ProcessId => _processId ;
87+
88+ /// <summary>
89+ /// Gets the name of the process.
90+ /// </summary>
91+ protected string ProcessName => _processName ;
92+
93+ /// <summary>
94+ /// Gets the name of the application.
95+ /// </summary>
96+ protected string AppName => _appName ;
97+
8398 #region Public Methods
8499
85100 /// <summary>
@@ -97,7 +112,7 @@ public override void TraceData(TraceEventCache eventCache, string source, TraceE
97112
98113 if ( ShouldTrace ( eventCache , source , eventType , id , null , null , data , null ) )
99114 {
100- var message = String . Empty ;
115+ var message = string . Empty ;
101116 if ( data != null )
102117 {
103118 message = data . ToString ( ) ;
@@ -150,7 +165,7 @@ public override void TraceData(TraceEventCache eventCache, string source, TraceE
150165 [ ComVisible ( false ) ]
151166 public override void TraceEvent ( TraceEventCache eventCache , string source , TraceEventType eventType , int id )
152167 {
153- TraceEvent ( eventCache , source , eventType , id , String . Empty ) ;
168+ TraceEvent ( eventCache , source , eventType , id , string . Empty ) ;
154169 }
155170
156171 /// <summary>
@@ -188,10 +203,10 @@ public override void TraceEvent(TraceEventCache eventCache, string source, Trace
188203
189204 if ( ShouldTrace ( eventCache , source , eventType , id , format , args ) )
190205 {
191- var message = String . Empty ;
206+ var message = string . Empty ;
192207 if ( args != null )
193208 {
194- message = String . Format ( CultureInfo . InvariantCulture , format , args ) ;
209+ message = string . Format ( CultureInfo . InvariantCulture , format , args ) ;
195210 }
196211 else
197212 {
@@ -213,37 +228,7 @@ public virtual void WriteTrace(TraceEventCache eventCache, string source, TraceE
213228 {
214229 WriteLine ( FormatTrace ( eventCache , source , eventType , id , message ) ) ;
215230
216- var includeOptions = ( ( int ) TraceOutputOptionsLevels & ( int ) eventType ) != 0 ;
217- if ( includeOptions && eventCache != null )
218- {
219- IndentLevel ++ ;
220- if ( ( TraceOptions . ProcessId & TraceOutputOptions ) != TraceOptions . None )
221- {
222- WriteLine ( String . Format ( CultureInfo . InvariantCulture , AssemblyResources . ProcessIdTraceToken , eventCache . ProcessId ) ) ;
223- }
224- if ( ( TraceOptions . LogicalOperationStack & TraceOutputOptions ) != TraceOptions . None )
225- {
226- var stack = StringHelpers . Join ( ", " , eventCache . LogicalOperationStack . ToArray ( ) ) ;
227- WriteLine ( String . Format ( CultureInfo . InvariantCulture , AssemblyResources . LogicalOperationStackTraceToken , stack ) ) ;
228- }
229- if ( ( TraceOptions . ThreadId & TraceOutputOptions ) != TraceOptions . None )
230- {
231- WriteLine ( String . Format ( CultureInfo . InvariantCulture , AssemblyResources . ThreadIdTraceToken , eventCache . ThreadId ) ) ;
232- }
233- if ( ( TraceOptions . DateTime & TraceOutputOptions ) != TraceOptions . None )
234- {
235- WriteLine ( String . Format ( CultureInfo . InvariantCulture , AssemblyResources . DateTimeTraceToken , eventCache . DateTime ) ) ;
236- }
237- if ( ( TraceOptions . Timestamp & TraceOutputOptions ) != TraceOptions . None )
238- {
239- WriteLine ( String . Format ( CultureInfo . InvariantCulture , AssemblyResources . TimestampTraceToken , eventCache . Timestamp ) ) ;
240- }
241- if ( ( TraceOptions . Callstack & TraceOutputOptions ) != TraceOptions . None )
242- {
243- WriteLine ( String . Format ( CultureInfo . InvariantCulture , AssemblyResources . CallstackTraceToken , eventCache . Callstack ) ) ;
244- }
245- IndentLevel -- ;
246- }
231+ WriteTraceOutput ( eventCache , eventType ) ;
247232 }
248233
249234 /// <summary>
@@ -308,13 +293,15 @@ protected virtual string FormatTrace(TraceEventCache eventCache, string source,
308293 namedArgs [ "EventType" ] = eventType . ToString ( ) ;
309294 namedArgs [ "EventId" ] = id ;
310295 namedArgs [ "Message" ] = message ;
311- namedArgs [ "ProcessId" ] = eventCache != null ? eventCache . ProcessId : 0 ;
312- namedArgs [ "ThreadId" ] = eventCache != null ? eventCache . ThreadId : String . Empty ;
296+ namedArgs [ "ProcessId" ] = ProcessId ;
297+ namedArgs [ "ProcessName" ] = ProcessName ;
298+ namedArgs [ "AppName" ] = AppName ;
299+ namedArgs [ "ThreadId" ] = eventCache != null ? eventCache . ThreadId : string . Empty ;
313300 namedArgs [ "ActivityId" ] = Trace . CorrelationManager . ActivityId ;
314- namedArgs [ "LogicalOperationStack" ] = String . Join ( ", " , Trace . CorrelationManager . LogicalOperationStack . ToArray ( ) . Select ( s => s . ToString ( ) ) . ToArray ( ) ) ;
301+ namedArgs [ "LogicalOperationStack" ] = string . Join ( ", " , Trace . CorrelationManager . LogicalOperationStack . ToArray ( ) . Select ( s => s . ToString ( ) ) . ToArray ( ) ) ;
315302 namedArgs [ "NewLine" ] = Environment . NewLine ;
316303
317- var result = String . Empty ;
304+ var result = string . Empty ;
318305
319306 try
320307 {
@@ -328,6 +315,52 @@ protected virtual string FormatTrace(TraceEventCache eventCache, string source,
328315 return result ;
329316 }
330317
318+ /// <summary>
319+ /// Writes the trace output.
320+ /// </summary>
321+ /// <param name="eventCache">The event cache.</param>
322+ /// <param name="eventType">Type of the event.</param>
323+ /// <param name="writeLine">The write line method. Default is <see cref="M:WriteLine"/>.</param>
324+ protected virtual void WriteTraceOutput ( TraceEventCache eventCache , TraceEventType eventType , Action < string > writeLine = null )
325+ {
326+ if ( writeLine == null )
327+ {
328+ writeLine = WriteLine ;
329+ }
330+
331+ var includeOptions = ( ( int ) TraceOutputOptionsLevels & ( int ) eventType ) != 0 ;
332+ if ( includeOptions && eventCache != null )
333+ {
334+ IndentLevel ++ ;
335+ if ( ( TraceOptions . ProcessId & TraceOutputOptions ) != TraceOptions . None )
336+ {
337+ writeLine ( string . Format ( CultureInfo . InvariantCulture , AssemblyResources . ProcessIdTraceToken , eventCache . ProcessId ) ) ;
338+ }
339+ if ( ( TraceOptions . LogicalOperationStack & TraceOutputOptions ) != TraceOptions . None )
340+ {
341+ var stack = StringHelpers . Join ( ", " , eventCache . LogicalOperationStack . ToArray ( ) ) ;
342+ writeLine ( string . Format ( CultureInfo . InvariantCulture , AssemblyResources . LogicalOperationStackTraceToken , stack ) ) ;
343+ }
344+ if ( ( TraceOptions . ThreadId & TraceOutputOptions ) != TraceOptions . None )
345+ {
346+ writeLine ( string . Format ( CultureInfo . InvariantCulture , AssemblyResources . ThreadIdTraceToken , eventCache . ThreadId ) ) ;
347+ }
348+ if ( ( TraceOptions . DateTime & TraceOutputOptions ) != TraceOptions . None )
349+ {
350+ writeLine ( string . Format ( CultureInfo . InvariantCulture , AssemblyResources . DateTimeTraceToken , eventCache . DateTime ) ) ;
351+ }
352+ if ( ( TraceOptions . Timestamp & TraceOutputOptions ) != TraceOptions . None )
353+ {
354+ writeLine ( string . Format ( CultureInfo . InvariantCulture , AssemblyResources . TimestampTraceToken , eventCache . Timestamp ) ) ;
355+ }
356+ if ( ( TraceOptions . Callstack & TraceOutputOptions ) != TraceOptions . None )
357+ {
358+ writeLine ( string . Format ( CultureInfo . InvariantCulture , AssemblyResources . CallstackTraceToken , eventCache . Callstack ) ) ;
359+ }
360+ IndentLevel -- ;
361+ }
362+ }
363+
331364 /// <summary>
332365 /// Determines if the trace listener should emit a trace statement based on the trace information provided.
333366 /// </summary>
@@ -453,7 +486,7 @@ protected void ApplyConfiguration()
453486 {
454487 if ( isRequired )
455488 {
456- throw new ConfigurationErrorsException ( String . Format ( CultureInfo . InvariantCulture , AssemblyResources . MissingRequiredAttribute , name ) ) ;
489+ throw new ConfigurationErrorsException ( string . Format ( CultureInfo . InvariantCulture , AssemblyResources . MissingRequiredAttribute , name ) ) ;
457490 }
458491 else if ( ! defaultValue . Equals ( nullValue ) )
459492 {
@@ -488,7 +521,22 @@ protected void ApplyConfiguration()
488521
489522 private bool _hasConfiguration = false ;
490523 private bool _isInitialized = false ;
491- private readonly object _initializeLock = new object ( ) ;
524+ private readonly object _initializeLock = new object ( ) ;
525+
526+ private static readonly int _processId ;
527+ private static readonly string _processName ;
528+ private static readonly string _appName ;
529+
530+ static TraceListenerBase ( )
531+ {
532+ using ( var process = Process . GetCurrentProcess ( ) )
533+ {
534+ _processId = process . Id ;
535+ _processName = process . ProcessName ;
536+ }
537+
538+ _appName = AppDomain . CurrentDomain . GetFriendlyName ( ) ;
539+ }
492540
493541 private const string _defaultTraceFormat = "{DateTime:yyyy-MM-dd} {DateTime:HH:mm:ss.FFF} {Message}" ;
494542 private const string _defaultTraceDataDelimiter = " " ;
0 commit comments