Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 ?

Use logging library like Nlog[^]. It will handle synchronization for you.
 
Share this answer
 
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?
[^]
 
Share this answer
 
Use lock statement[^] for solving Thread Synchronization issues and force the code block to execute one thread at a time.
 
Share this answer
 
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 :

XML
<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 :

C#
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900