Logging Linq to SQL operations

1 בפברואר 2010

תגיות: ,
60 תגובות

Linq to SQL provide build in logger that logs all select, insert, update and delete operations, it executes. Very helpful tool when something goes wrong.

You can find it under DataContext .Log property that takes TextWriter .

Most of the samples you probably would find about the logger are setting the Console.Out to it, like this:

DataContext dataContext = newMyDataContext();
dataContext.Log = Console.Out;

The more common scenario would be to write it to a text file. Changing this code to open a StreamWriter is not so hand:

string logFilePath = @"c:\log.txt";
FileStream fileStream = new FileStream(logFilePath, FileMode.OpenOrCreate, FileAccess.Write);
TextWriter textWriter = new StreamWriter(fileStream);
dataContext.Log = textWriter;

Unfortunately this code will not work properly all the time, not if you use Linq to SQL with Async calls or Parallel execution. If you have more then one thread that accessing the same file you probably get the exception "The process cannot access the file because it is being used by another process".

To overcome this problem all that need to be done is opening the file with sharing, using FileShare enum, which is the proper way to handle files when running multi threads

FileStream fileStream = new FileStream(logFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
הוסף תגובה
facebook linkedin twitter email

להגיב על Barbour לבטל

האימייל לא יוצג באתר. שדות החובה מסומנים *

60 תגובות

  1. Shani12 בפברואר 2010 ב 11:35

    I am not sure that this is the best way.
    1) logging in real-time will make you app work slow
    2) writing to file instead of DB is harder to read\filter

    I think that first you should write to DB.
    if you can it will even be better thinking of the log as in-memory first and persist to lg table later as bulk.

    have fun.

    הגב
  2. TB27 בפברואר 2011 ב 15:08

    Any ideas why this code would not work for me?
    I get the select statement printed out through the console, but when I switch to the log file as proposed here (copy, paste), nothing happens. Well, actually the file gets generated, but nothing is writen into it…

    הגב
  3. Csharper27 בפברואר 2011 ב 15:24

    OK, I had problems with writting to a file – it turned out I have to call

    textWriter.Flush()

    after performing any action to actually get the text into a file. Any ideas why this is required? Or is it normal?

    הגב
  4. Aponte29 ביולי 2012 ב 0:18

    well, i do believe in astrology and horoscope is the first part of that the magazine that i always check

    הגב
  5. Castleberry1 באוגוסט 2012 ב 17:56

    some energy bars have been just too sweet for my own
    taste. is there a sugar at no cost energy bar

    הגב
  6. Strong2 באוגוסט 2012 ב 8:10

    Have been your clients familiar using the web, do that they utilize it
    routinely

    הגב
  7. Ayres4 באוגוסט 2012 ב 23:30

    “Thanks so much for this! I have not been this moved by a blog for a long time!

    הגב
  8. Colby6 באוגוסט 2012 ב 22:09

    The whole thing you must identify about News is over here.

    הגב
  9. Horvath11 באוגוסט 2012 ב 19:27

    Is there a toughest thing in the world To believe.

    הגב
  10. Lassiter12 באוגוסט 2012 ב 8:26

    pay per click programs are really great, i could earn some decent money from it,,

    הגב
  11. Perea12 באוגוסט 2012 ב 8:28

    the table beds which we have a year ago had already broken down, its mostly
    made up of plastic.,

    הגב
  12. Emery12 באוגוסט 2012 ב 8:28

    This is actually that the simplest and best guidance I
    have ever come across about this topic. Thank you for this very educational blog
    post of yours. Also, I enjoy writing articles which has a personal tone incorporated.
    I feel it makes your reader feel more important and also inclined to
    believe me more. Plus it appears to be more real and not coming out of a robot.

    I love making my visitors feel important and also special.
    I want to give away them the best tips on how to
    handle important issues such as this.

    הגב
  13. Acuna12 באוגוסט 2012 ב 8:29

    This is mostly a appealing post by that the tactic. I am going to go ahead and
    save this article for my brother to check out later on tomorrow.
    Keep up that the highquality work.

    הגב
  14. Pridgen12 באוגוסט 2012 ב 8:30

    Isnt it entertaining if we always talk about topics for example that.

    הגב
  15. Nicholson12 באוגוסט 2012 ב 8:34

    i love food and drinks that have been very tasty.
    i do eat a ton and drink a whole lot of alcohol currently

    הגב
  16. Anonymous12 באוגוסט 2012 ב 8:36

    Hallo super toller Blog den ihr da habt. Bin gerade über die MSN Suche darüber gestolpert.
    Gefällt mir echt super gut. macht weiter so. MFG Susi

    הגב
  17. Menendez12 באוגוסט 2012 ב 9:05

    You made some decent points there. I looked on line for any question and found most people may go
    along with with your web site.

    הגב
  18. Tavares12 באוגוסט 2012 ב 9:29

    I always visit new blog everyday and i found your blog.

    הגב
  19. Bigelow12 באוגוסט 2012 ב 9:57

    You should experience a tournament for just one of the best domains
    on that the internet. Let me suggest this website!

    הגב
  20. Caballero12 באוגוסט 2012 ב 10:08

    The thing i for instance about your blog is you to always post direct to that the point info.

    הגב
  21. Hatley12 באוגוסט 2012 ב 10:18

    both silver jewelry and gold jewelry have been good because body adornments,
    gold is of course the best and silver is second

    הגב
  22. Henning16 באוגוסט 2012 ב 21:47

    This sort of thing needs to happen! Merely letting
    the quota happen isnt acceptable. This will help you stay above
    the curve.

    הגב
  23. Willoughby20 באוגוסט 2012 ב 9:28

    we use wall arts at home because it is a very good and stylish decoration to add at your home“

    הגב
  24. Rasmussen23 באוגוסט 2012 ב 4:03

    I have watched “boston legal ever since in my college days, i love to watch courtroom drama,.

    הגב
  25. Peralta23 באוגוסט 2012 ב 7:20

    I must say, I think your actually on point with this, I
    cant say I am completely on that the same page, then again it's not big of a deal .

    הגב
  26. Fontaine23 באוגוסט 2012 ב 9:17

    Quite several on the web communities include singles sets which
    attempt enjoyable click here activities mutually, all this
    is actually is usually a first-class renewable dating way.
    Incidents similar to pedaling, bowling, curling,
    video clip evenings, breaking a leg and comedy club sets tend to be organized with the
    singles group, conversely it enables a evenlydistributed group of objectives
    to have a great as well as laidback nights. Together with emphasis apply to
    the actual activity themselves as an additional to building
    a romantic connection, you will need plenty of demand journey singles in addition to sights manifest additional the natural system throughout this kind of environment.

    הגב
  27. Pinkney23 באוגוסט 2012 ב 13:08

    Would you mind me wondering where you got that the graphic in this
    post fromstyle.

    הגב
  28. Blake31 באוגוסט 2012 ב 4:35

    Thanks a lot for this particular material I had been browsing all Search engines
    to get it!

    הגב
  29. Cecil5 בספטמבר 2012 ב 21:09

    watching on that the net movies has been my past time this month, i actually enjoy it

    הגב
  30. Delossantos9 בספטמבר 2012 ב 7:15

    It can be not that the case that reader must be completely agreed with authors views about article.
    So this is what happened with me, anyways it is a first-class effort,
    I appreciate it. Thanks

    הגב
  31. Button13 בספטמבר 2012 ב 2:18

    It definitely felt along the lines of an ode to the Marine Corps.

    הגב
  32. Dorsey16 בספטמבר 2012 ב 6:24

    This post has some really wonderful insights and interpretations in it.
    How long have you been blogging for?

    הגב
  33. Joy16 בספטמבר 2012 ב 9:23

    Well, Objective, im not so i have been in agreement with all in this article, on that the other hand
    you do make a important comprehension with this issue. I may instruct
    myself

    הגב
  34. Perea16 בספטמבר 2012 ב 18:17

    This site post is good, probably because of how well the subject was developped.
    I something like some of the comments too though I could
    prefer we all stay on the suject in order add value to that the subject!

    הגב
  35. Keister16 בספטמבר 2012 ב 20:17

    All of the novels of John Grisham are very
    good, i love all the stories..

    הגב
  36. Richter16 בספטמבר 2012 ב 20:53

    It actually is rare to come across a specialist in whom you'll have some confidence. In that the world in the give away day, nobody genuinely cares about showing others the best method in this matter. How fortunate I am to have found a real wonderful blog because this. Its people like you exactly who make a true difference currently through that the concepts that they write about.

    הגב
  37. Quinones17 בספטמבר 2012 ב 0:00

    Judging by that the system you write, you seem along the lines of a professional writer.
    ,

    הגב
  38. Winter17 בספטמבר 2012 ב 3:00

    Im not sure where youre getting your info, then again great
    topic. I needs to spend some time learning more or understanding more.
    Thanks for fantastic info I was looking for this information for my mission.

    הגב
  39. Roth17 בספטמבר 2012 ב 7:25

    it can be wise to choice to choose Light Emitting Diode christmas lights becaue that they are not fire hazard’

    הגב
  40. Woodall17 בספטמבר 2012 ב 17:32

    Augustine thanks for sharing this! My extensive google search has now been recently paid for using quality insight to talk about together
    with our relatives.

    הגב
  41. Easter17 בספטמבר 2012 ב 19:02

    Wow, you seem to be very knowledgable about this kind of topics.
    ’~,-:

    הגב
  42. Dorsey18 בספטמבר 2012 ב 17:18

    very first-class post, i definitely love this site, keep on it

    הגב
  43. Ainsworth19 בספטמבר 2012 ב 19:29

    there are quite a large amount of domains via internet that offers
    downloadable movies, some have been even offering the latest movies.

    הגב
  44. Spears19 בספטמבר 2012 ב 19:56

    Luckily for us and that the cast, they also got to work with great pacing, a fun script, and
    fresh dark humor that can inject life in this dismal summer season.

    הגב
  45. Quintanilla21 בספטמבר 2012 ב 23:21

    I always visit new blog everyday and i found your blog.

    הגב
  46. Barbour21 בספטמבר 2012 ב 23:23

    I am glad that I noticed this web blog, precisely that the right information that
    I was searching for!

    הגב
  47. Fite21 בספטמבר 2012 ב 23:36

    Folks nowadays really needs to be even more a ton more certified
    to celebrate the benefits of your problems, to implement provide day methods
    for contraceptive, to store all of our all-natural advice additionally
    recycle for buck all of our products and answers and advice.

    We’re also guaranteed we’ll have obtained a
    enhanced in addition tidier web-site if you want it.

    הגב
  48. Coggins21 בספטמבר 2012 ב 23:37

    Once talking for some time plus getting to be acquainted with each
    other, it would be pure to fulfill. It becomes some
    sort of rather shallow connection if you and him tend to
    be content to end up being for a long time simply chatting.

    Each men and women might be seriously looking another individual to help speak with.

    הגב
  49. High22 בספטמבר 2012 ב 0:26

    well, skin cancer incidence would be increasing because of the hole
    on the ozone layer”

    הגב
  50. Starr22 בספטמבר 2012 ב 9:11

    getting a masters degree is of course necessary when you
    want a wage increase and improvement in your career.
    ,

    הגב
  51. Frey22 בספטמבר 2012 ב 14:42

    LED floor lamps have been the newest design when it comes to lighting the floor area.

    they don't run as hot because traditional floor lamps,,

    הגב
  52. Parnell22 בספטמבר 2012 ב 18:59

    I am curious to seek out out what blog manner youre working with Im experiencing some tiny security problems with my latest blog and
    Id for instance to search for something more safe.

    Do you have any other suggestions

    הגב
  53. Millard22 בספטמבר 2012 ב 19:23

    Its tough to get knowledgeable people within this topic, on that the
    other hand the truth is could be seen because what happens you are preaching about!
    Thanks

    הגב
  54. Chew22 בספטמבר 2012 ב 21:15

    my dad is really a massage therapist and he can actually relieve small pains and injuries

    הגב
  55. Dawkins22 בספטמבר 2012 ב 21:27

    You cant discover a cup of tea big sufficient or
    a book long enough to suit me.

    הגב
  56. Evans22 בספטמבר 2012 ב 22:12

    i love new product launches, i always attend events just like those because i would for example to see some new stuffs“

    הגב
  57. Shell22 בספטמבר 2012 ב 22:20

    this year our home decorating ideas would be more on eco
    friendly home decorations,,

    הגב
  58. Houser13 באוקטובר 2012 ב 2:07

    As a Newbie, I am always exploring on line for articles that can be of assistance to
    me. Thank you

    הגב
  59. Shockley11 בנובמבר 2012 ב 19:45

    Have you already setup a fan page on Facebook ?-’~`:

    הגב
  60. Orourke7 בפברואר 2013 ב 20:51

    Truly no matter if someone doesn't know then its up to other people that they will assist, so here it happens.

    הגב