Click here to Skip to main content
14,301,484 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,


I want to create a trace log file. Like in the whole project in every function,s entry point and at the exit point, I want to write some text in a text file. And in my application I am using a threading to do some other task, but due to this thread we are unable to write on that trace log file. It gives the message somethig like 'you can not use trace log file because that trace log file is being used by another processor'. Can anybody tell me the solution of this problum.


Thanks
Posted
Comments
Amir Mahfoozi 27-Jun-12 0:47am
   
How are you ?
Rate this:
Please Sign up or sign in to vote.

Solution 1

Use logging library like Nlog[^]. It will handle synchronization for you.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

There are few options like

log4net[^]
nlog[^]
The Logging Application Block[^]

These tools are designed for logging and tracing. You can use one of them.
Some other links:

.NET logging framework
[^]
Tracing versus Logging and how does log4net fit in?
[^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Use lock statement[^] for solving Thread Synchronization issues and force the code block to execute one thread at a time.
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

Hi,

You should synchronize two threads to let them interfere with each other.

Here is an example to show how to do this :

The XAML part :

<Grid>
    <Button Content="Illegal Access" Height="23" HorizontalAlignment="Left" Margin="191,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <ListBox Height="117" HorizontalAlignment="Left" Margin="20,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" />
    <Button Content="Legal Access" Height="23" HorizontalAlignment="Left" Margin="192,58,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
</Grid>


And the code :

private void button1_Click(object sender, RoutedEventArgs e)
{
    Thread thread = new Thread(new ThreadStart(IllegalAccessThread));

    thread.Start();
}

void IllegalAccessThread()
{
    listBox1.Items.Add("message");
}

private void button2_Click(object sender, RoutedEventArgs e)
{
    Thread thread = new Thread(new ParameterizedThreadStart(LegalAccessThread));

    thread.Start(SynchronizationContext.Current);
}

void LegalAccessThread(object param)
{
    SynchronizationContext context = (param as SynchronizationContext);

    for (int i = 0; i < 10; i++)
    {
        context.Send(delegate(object state)
        {
            listBox1.Items.Add(string.Format("message {0} from another thread", i));
        }, "");

        Thread.Sleep(1000);
    }
}



You should pass current SynchronizationContext to other threads to let them run their code in your context by using Send and Post functions.

I hope this helps you.

Good Luck.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100