Tag: CSharp

  • Reducing HttpClient Logging in C# Application

    If you have an application code written in C# and use HttpClient in the code, you may encounter instances where HttpClient generates a significant amount of logging information, such as:

    • Start processing HTTP request
    • Sending HTTP request
    • Received HTTP response headers after … ms – 200
    • End processing HTTP request after … ms – 200

    These logs can be overwhelming and make it difficult to identify and troubleshoot issues. In this blog post, we’ll show you how to adjust the logging levels for HttpClient to reduce the amount of logging information generated by your application.

    To remove the logging that you’re seeing in your C# code, you can adjust the logging level for the System.Net.Http.HttpClient namespace in your appsettings.json file. By setting the logging level to Warning or higher, you can prevent the Info level logs from appearing.

    Here’s an example of how you can adjust the logging level in your appsettings.json file:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "System.Net.Http.HttpClient": "Warning"
        }
      }
    }
    

    In the above example, the logging level for System.Net.Http.HttpClient is set to Warning, which means that only Warning, Error, and Critical level logs will be displayed for that namespace. The Info level logs will no longer appear.

    After making these changes, restart your application for the new logging settings to take effect.

  • CSharp – Get Assembly and File Version

    If you want to know the version of currently executing managed assembly, you can use Reflection.Assembly.

    Here is code snippet which can be used to do get managed assembly version.

    private string GetVersion()
    {
          return Assembly.GetExecutingAssembly().GetName().Version.ToString();
    }

  • How to Validate URL in C#

    I searched google for how to validate URL in C# and most results say using a regular expression. Eventually I found Uri.TryCreate which is a built in method in C# to check if a string is a valid URL

    Uri uri = null;

    if (!Uri.TryCreate(url, UriKind.Absolute, out uri) || null == uri)

    {

        //Invalid URL

        return false;

    }

     

    Reference:

    http://msdn.microsoft.com/en-us/library/ms131572(v=VS.90).aspx

  • Use coalescing operator to write more readable code

    Tired of code like this?

    public string Foo

    {

    get { return foo; }

    set

    {

    if (value == null)

    value = String.Empty;

    foo = value;

    }

    }

    public string Bar

    {

    get { return bar; }

    set

    {

    if (value == null)

    value = String.Empty;

    bar = value;

    }

    }

    Use coalescing operator to make your code a bit more readable.

    (more…)

  • C# reading from and write to text file

    CSharp sample for reading from a text file and writing to a text file.

    //Reading from a text file      

    System.IO.StreamReader srFile = new System.IO.StreamReader(@"c:foo.txt");

    string str = srFile.ReadToEnd();

    srFile.Close();

     

    //Writing to a text file

    string lines = "foobar";

    System.IO.StreamWriter swFile = new System.IO.StreamWriter(@"c:bar.txt");

    swFile.WriteLine(lines);

    swFile.Close();