Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. __LINE__

__LINE__

Scheduled Pinned Locked Moved C#
csharpc++question
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rick Crone
    wrote on last edited by
    #1

    In C++ we have a macro __LINE__ that can be used to report what line of source code we are on. Is there a way to do this in C#?

    R 1 Reply Last reply
    0
    • R Rick Crone

      In C++ we have a macro __LINE__ that can be used to report what line of source code we are on. Is there a way to do this in C#?

      R Offline
      R Offline
      Rick Crone
      wrote on last edited by
      #2

      I found this: CodeLinePragma clp = new CodeLinePragma("Service1.cp",188); but it doesn't seem to keep up with the line number... it just reports back whatever I initialize it with.

      H 1 Reply Last reply
      0
      • R Rick Crone

        I found this: CodeLinePragma clp = new CodeLinePragma("Service1.cp",188); but it doesn't seem to keep up with the line number... it just reports back whatever I initialize it with.

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        That's just the CodeDom class that represents the #line preproc instruction, mostly used in debugging and error/warning reporting during compilation, if one was to strip the lines for some stupid reason. What do you need something like this for? If it's for debugging purposes, exceptions will already contain line numbers for assemblies compiled with debugging information. When doing release builds (i.e., DEBUG is not defined), this information is stripped. If you're doing it for some kind of logging, I don't know what to tell you. csc.exe doesn't doing anything like that based on the documentation. If using VS.NET 2003, you could have a pre-build step that runs a macro to replace some line with the line number (I would think that would be available through the scripting object model in VS.NET).

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        R L J 3 Replies Last reply
        0
        • H Heath Stewart

          That's just the CodeDom class that represents the #line preproc instruction, mostly used in debugging and error/warning reporting during compilation, if one was to strip the lines for some stupid reason. What do you need something like this for? If it's for debugging purposes, exceptions will already contain line numbers for assemblies compiled with debugging information. When doing release builds (i.e., DEBUG is not defined), this information is stripped. If you're doing it for some kind of logging, I don't know what to tell you. csc.exe doesn't doing anything like that based on the documentation. If using VS.NET 2003, you could have a pre-build step that runs a macro to replace some line with the line number (I would think that would be available through the scripting object model in VS.NET).

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          R Offline
          R Offline
          Rick Crone
          wrote on last edited by
          #4

          Thank you for your reply! I'm currently working on some services and I'm not seeing the line number when I catch an exception. Maybe it's because I handled the exception - by exiting my service but this is an example of what my exception reports: System.Data.SqlClient.SqlException: SHUTDOWN is in progress. Log off. at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) at GetAcc.GetAcc.timer1_Elapsed(Object sender, ElapsedEventArgs e) I want the line number so I know where (which exception trap) trapped the error. In a case like the example above (depending on what step I'm in in my process) I may be able to keep my service alive until the SQL server is available again. I have found that I can use Environment.StackTrace which gives more info... but does include the line number. So I'm adding that to my message that I log to a file.

          H 1 Reply Last reply
          0
          • R Rick Crone

            Thank you for your reply! I'm currently working on some services and I'm not seeing the line number when I catch an exception. Maybe it's because I handled the exception - by exiting my service but this is an example of what my exception reports: System.Data.SqlClient.SqlException: SHUTDOWN is in progress. Log off. at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) at GetAcc.GetAcc.timer1_Elapsed(Object sender, ElapsedEventArgs e) I want the line number so I know where (which exception trap) trapped the error. In a case like the example above (depending on what step I'm in in my process) I may be able to keep my service alive until the SQL server is available again. I have found that I can use Environment.StackTrace which gives more info... but does include the line number. So I'm adding that to my message that I log to a file.

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            If you do a debug build, you should get line numbers for your assemblies (.NET BCL assemblies obviously aren't debug assemblies). I take it you're testing this with a release build? You also have to use the same path for the PDB files (debug files) that were created when you compiled. That contains the information. If you switch to a different machine (unless you keep the path to the copied PDB files the same), you don't see this. Does that ring a bell at all?

            -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

            R 1 Reply Last reply
            0
            • H Heath Stewart

              That's just the CodeDom class that represents the #line preproc instruction, mostly used in debugging and error/warning reporting during compilation, if one was to strip the lines for some stupid reason. What do you need something like this for? If it's for debugging purposes, exceptions will already contain line numbers for assemblies compiled with debugging information. When doing release builds (i.e., DEBUG is not defined), this information is stripped. If you're doing it for some kind of logging, I don't know what to tell you. csc.exe doesn't doing anything like that based on the documentation. If using VS.NET 2003, you could have a pre-build step that runs a macro to replace some line with the line number (I would think that would be available through the scripting object model in VS.NET).

              -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              Heath Stewart wrote: What do you need something like this for? If it's for debugging purposes, exceptions will already contain line numbers for assemblies compiled with debugging information. When doing release builds (i.e., DEBUG is not defined), this information is stripped. If one generated some code from some other source, its allways helpful to make #line references. Think lex & yacc for example :) leppie::AllocCPArticle("Zee blog");
              Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

              1 Reply Last reply
              0
              • H Heath Stewart

                That's just the CodeDom class that represents the #line preproc instruction, mostly used in debugging and error/warning reporting during compilation, if one was to strip the lines for some stupid reason. What do you need something like this for? If it's for debugging purposes, exceptions will already contain line numbers for assemblies compiled with debugging information. When doing release builds (i.e., DEBUG is not defined), this information is stripped. If you're doing it for some kind of logging, I don't know what to tell you. csc.exe doesn't doing anything like that based on the documentation. If using VS.NET 2003, you could have a pre-build step that runs a macro to replace some line with the line number (I would think that would be available through the scripting object model in VS.NET).

                -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                J Offline
                J Offline
                J Dunlap
                wrote on last edited by
                #7

                Heath Stewart wrote: if one was to strip the lines for some stupid reason. One place this is done is in ASP.NET code that's embedded in the page. ;)

                **"Love does not delight in evil but rejoices with the truth." -- 1 Corinthians 13:6

                FLUID UI Toolkit | FloodFill in C# & GDI+**

                1 Reply Last reply
                0
                • H Heath Stewart

                  If you do a debug build, you should get line numbers for your assemblies (.NET BCL assemblies obviously aren't debug assemblies). I take it you're testing this with a release build? You also have to use the same path for the PDB files (debug files) that were created when you compiled. That contains the information. If you switch to a different machine (unless you keep the path to the copied PDB files the same), you don't see this. Does that ring a bell at all?

                  -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                  R Offline
                  R Offline
                  Rick Crone
                  wrote on last edited by
                  #8

                  Oh... ok I am using debug builds... but I have not copied PDB file to the production machine. When I tested Environment.StackTrace I was on my development machine. I bet I won't get line numbers from that either on the production machine. Thanks again!

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups