question

PadmanabhanVenkatesh-6789 avatar image
0 Votes"
PadmanabhanVenkatesh-6789 asked SimpleSamples answered

check if a text exists in a file

Hi .

I have a custom log file , and am trying to send the log file in email, if the file contains text "error" or "bcp copy out failed".

I have tried this code, but getting an error in the contains check.

 string loggerfilepath  = "c://abc.txt";
    
   if ( ( File.ReadAllText(loggerfilepath).Contains("error", StringComparison.CurrentCultureIgnoreCase)) || (File.ReadAllText(loggerfilepath).Contains("BCP copy out failed", StringComparison.CurrentCultureIgnoreCase)))
             {  
 // do other activity
 }

How to fix this? Thanks

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SimpleSamples avatar image
0 Votes"
SimpleSamples answered

Try the following, except I have two if statements and you can combine the two conditions into one if statement as you had.

 string loggerfiletext = File.ReadAllText(loggerfilepath);
 loggerfiletext = loggerfiletext.ToLower();
 if ((loggerfiletext.Contains("error")))
     Console.WriteLine("Contains error");
 else
     Console.WriteLine("Does not contain ERROR");
 if ((loggerfiletext.Contains("bcp copy out failed")))
     Console.WriteLine("Contains bcp copy out failed");
 else
     Console.WriteLine("Does not contain bcp copy out failed");

See the String.Contains Method documentation. The reason you were getting an error on Contains is that there is not an overload for specifying a StringComparison argument.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered

Copied from Google =>

Top level Class :

 public static class StringExtensions
 {
     public static bool Contains(this string source, string toCheck, StringComparison comp)
     {
         return source?.IndexOf(toCheck, comp) >= 0;
     }
 }  

then :

 string loggerfilepath = "c://abc.txt";
 if ((System.IO.File.ReadAllText(loggerfilepath).Contains("error", StringComparison.CurrentCultureIgnoreCase)) || ((System.IO.File.ReadAllText(loggerfilepath).Contains("BCP copy out failed", StringComparison.CurrentCultureIgnoreCase))))
 {
     // do other activity   
 }



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered PadmanabhanVenkatesh-6789 commented

Try using the following which is cleaner and easy to debug. Place the class Extensions in it's own file and have a using statement in the code to use it. Note you were reading file twice, only need to read the file once.

 using System;
 using System.IO;
 using System.Linq;
    
 namespace ConsoleNetCoreApp1
 {
     class Program
     {
         static void Main(string[] args)
         {
             string loggerfilepath = "c://abc.txt";
    
             string fileContents = File.ReadAllText(loggerfilepath);
             if (fileContents.ContainsAny("error", "bcp copy out failed"))
             {
                    
             }
         }
    
     }
    
     public static class Extensions
     {
         public static bool ContainsAny(this string sender, params string[] tokens)
         {
             foreach (string token in tokens)
             {
                 if (sender.Contains(token, StringComparison.CurrentCultureIgnoreCase))
                 {
                     return true;
                 }
             }
    
             return false;
         }
     }
 }


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks. Tried the code -

Getting error as : No overload for method 'Contains' takes 2 arguments

0 Votes 0 ·