Trace file isn't being created even though TraceEvent is called?

邮差的信 提交于 2019-11-27 08:44:43

问题


Objective

Use System.Diagnostics to perform tracing. Though I have used log4net and other logging solutions, I am only interested in getting tracing to work with System.Diagnostics.

Problem

Even though I'm issuing the TraceEvent the file is not being created anywhere.

Application Information

I have an application that's hosting some WF services. One of the services is a state machine with an initial state that looks like this:

the LogMessage custom activity is also very straight forward. It receives four basic parameters:

defines the TraceSource as a variable:

and then simply calls TraceEvent:

Configuration

The configuration for this TraceSource and TraceListener is as follows:

  <system.diagnostics>
    <trace autoflush="true"/>
    <sources>
      <source name="log" switchValue="All">
        <listeners>
          <add name="file" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
               BaseFileName="gsf_workflows.txt"
               DiskSpaceExhaustedBehavior="ThrowException"
               Location="Custom"
               CustomLocation="D:\Log"
               MaxFileSize="81920000"
               LogFileCreationSchedule="LogFileCreationScheduleOption.Daily"/>
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

回答1:


FYI for everyone stumbling on this page..

FileLogTraceListener will not create folders if they are missing.

FileLogTraceListener will not bypass security, your process identity will need create+modify permissions on the target folder.

It's "bad form" to target the executable folder, this tends to change depending on the host (for example, when writing under IIS Express you aren't writing to the same location as DevEnv, nor the hosted web app.) One suggestion is that instead of "ExecutableLocation" you may want to opt for "Custom" and specify a particular path (such as X:\logfiles)

Writers flush to disk when their internal buffers fill, if memory serves me the default buffer size is 8KB. This is not a facet of trace listeners, but the underlying file stream it writes to.

Lastly, if you do not need the form and function of FileLogTraceListener, instead consider tracing to the EventLog (there is a listener for this) as this may be more accessible to other devs and non-devs (such as ops engineers, third party monitoring tools, etc.)




回答2:


Two things:

1) Your workflow is most likely stopping due to exceptions because your .config file is not right. Note the changes:

<system.diagnostics>
  <sources>
    <source name="log" switchValue="All">
      <listeners>
        <add name="file" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, 
                         Microsoft.VisualBasic, Version=10.0.0.0, 
                         Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
             BaseFileName="gsf_workflows.txt"
             DiskSpaceExhaustedBehavior="ThrowException"
             Location="ExecutableDirectory"
             MaxFileSize="81920000"
             LogFileCreationSchedule="Daily"/>
      </listeners>
    </source>
  </sources>
</system.diagnostics>

2) You've to flush the listeners, otherwise they won't be written. You can do it in two ways.

Explicitly call Flush method on your logger variable using again InvokeMethod activity.

OR

Turn on auto flush on config file:

<system.diagnostics>
    <trace autoflush="true"/>
    ....
</system.diagnostics>

That being said, I don't know if you're aware of Workflow Tracking and Tracing capabilities. It looks a good fit for your needs. Check this answer for more links and examples.



来源:https://stackoverflow.com/questions/14502960/trace-file-isnt-being-created-even-though-traceevent-is-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!