Site Logfile Explorer

english    german

Home Support Purchase Faq Downloads Contact

IIS log file download with asp.net webservice (.asmx)

c# - sample

WebService1.asmx.cs

////////////////////////////////////////////////////////////////////////

using System.Web;
using System.Web.Services;
using System.Collections;
using System;
using System.IO;

namespace WebService1
{
/// <summary>
/// Zusammenfassungsbeschreibung für "Service1"
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
string LfDir = "C:\\inetpub\\logs\\LogFiles\\W3SVC1\\";

[WebMethod]
public ArrayList GetLogfileList(Double LstLfCount)
{
ArrayList LfAl = new ArrayList();

/// Check, if the connection is secure
if (HttpContext.Current.Request.IsSecureConnection)
return null;

DateTime Dt = DateTime.Now;

for (double d = LstLfCount - 1.0; (d >= 0.0); d--)
{
string LfNm = GetLogfileName(Dt.AddDays(d * -1));

if (File.Exists(LfDir + LfNm))
{

Logfile Lf = new Logfile();

Lf.LogfileName = LfNm;
Lf.LastWriteTime = new FileInfo(LfDir + LfNm).LastWriteTime;
LfAl.Add(Lf);
}
}

return LfAl;
}

[WebMethod]
public Logfile GetLogfile(string LfName)
{

Logfile Lf = new Logfile();

/// Check, if the connection is secure
if (HttpContext.Current.Request.IsSecureConnection)
return null;


if (File.Exists(LfDir + LfName))
{
FileStream fs = new FileStream((LfDir + LfName), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

StreamReader sr = new StreamReader(fs);

string LfStr = sr.ReadToEnd();

fs.Close();
Lf.LogfileText = LfStr;
}
return Lf;
}

String GetLogfileName(DateTime LfDt)
{
string Dty = LfDt.Year.ToString().Substring((LfDt.Year.ToString().Length - 2));
string Dtm = LfDt.Month.ToString();
string Dtd = LfDt.Day.ToString();

if (Dtm.Length == 1)
Dtm = "0" + Dtm;

if (Dtd.Length == 1)
Dtd = "0" + Dtd;

return "u_ex" + Dty + Dtm + Dtd + ".log";

}
}
}
public class Logfile
{
public string LogfileName;
public DateTime LastWriteTime;
public string LogfileText;

public Logfile()
{
}
}

////////////////////////////////////////////////////////////////////////


form1.cs

////////////////////////////////////////////////////////////////////////

using System;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

string LocDir = "c:\\Users\\xxxxxx\\Desktop\\W3SVC2\\";

Lf.Service1 Ws = new Lf.Service1();

Object[] Al = Ws.GetLogfileList(5.0);

foreach (object o in Al)
{
Lf.Logfile Lf = (Lf.Logfile) o;

bool Dl = false;

string Lfloc = LocDir + Lf.LogfileName;

if (File.Exists(Lfloc))
{
if (new FileInfo(Lfloc).LastWriteTime < Lf.LastWriteTime )
Dl = true;
}
else
{
Dl = true;
}

if (Dl == true)
{
object LfNT = Ws.GetLogfile(Lf.LogfileName);
string LfN = LfNT as string;
object LfTextT = Ws.GetLogfile(Lf.LogfileText);
string LfText = LfTextT as string;
File.WriteAllText(Lfloc, LfText);
File.SetLastWriteTime(Lfloc, Lf.LastWriteTime);
label1.Text += Lf.LogfileName + System.Environment.NewLine;
}
}
}
}
}

////////////////////////////////////////////////////////////////////////

vb.net sample

Webservice.asmx.vb

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Imports System.Web.Services
Imports System.ComponentModel
Imports System.IO

<System.Web.Services.WebService(Namespace:="http://tempuri.org/")>
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
<ToolboxItem(False)>
Public Class LfDownload
Inherits System.Web.Services.WebService

Private LfDir As String = "C:\inetpub\logs\LogFiles\W3SVC1\"

<WebMethod()>
Public Function GetLogfileList(LstLfCount As Double) As ArrayList

Dim LfAl As New ArrayList

Try
' Check, if then connection is secure
If Not HttpContext.Current.Request.IsSecureConnection Then Return Nothing

Dim Dt As Date = Now

For d As Double = LstLfCount - 1 To 0.0 Step -1.0

Dim LfNm As String = GetLogfileName(Dt.AddDays(d * -1))

If File.Exists(LfDir & LfNm) Then

Dim Lf As New Logfile

Lf.LogfileName = LfNm
Lf.LastWriteTime = New FileInfo(LfDir & LfNm).LastWriteTime
LfAl.Add(Lf)

End If

Next

Return LfAl

Catch ex As Exception
Dim Msg As New ArrayList
Msg.Add(ex.Message)
Return Msg
End Try

End Function

<WebMethod()>
Public Function GetLogfile(LfName As String) As Logfile

Dim Lf As New Logfile

Try
' Check, if then connection is secure
If Not HttpContext.Current.Request.IsSecureConnection Then Return Nothing

If File.Exists(LfDir & LfName) Then

Dim fs As New FileStream(LfDir & LfName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)

Dim sr As New StreamReader(fs)

Dim LfStr As String = sr.ReadToEnd

fs.Close()

Lf.LogfileText = LfStr

End If

Catch ex As Exception
Lf.LogfileText = ex.Message
End Try

Return Lf

End Function

Private Function GetLogfileName(LfDt As Date) As String

Try
Dim Dty As String = Microsoft.VisualBasic.Right(LfDt.Year.ToString, 2)
Dim Dtm As String = LfDt.Month.ToString
If Dtm.Length = 1 Then Dtm = "0" & Dtm
Dim Dtd As String = LfDt.Day.ToString
If Dtd.Length = 1 Then Dtd = "0" & Dtd

Dim Dt As String = Dty & Dtm & Dtd

Return "u_ex" & Dt & ".log"

Catch ex As Exception
Return ex.Message
End Try

End Function

End Class

Public Class Logfile
Public LogfileName As String
Public LastWriteTime As DateTime
Public LogfileText As String
Public Sub New()
End Sub
End Class
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Form1.vb
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Imports System.IO

Public Class Form1

Private WbService As New LfDl.LfDownload

Public Sub New()

' Dieser Aufruf ist für den Designer erforderlich.
InitializeComponent()

' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

Try
Dim LocDir As String = "c:\Users\xxxxx\Desktop\W3SVC2\"

Dim Msg As String = ""

Dim Lfs As Object = WbService.GetLogfileList(7)

For Each Lf As LfDl.Logfile In Lfs

Dim Dl As Boolean = False

Dim Lfloc As String = LocDir & Lf.LogfileName

If File.Exists(Lfloc) Then
If New FileInfo(Lfloc).LastWriteTime < Lf.LastWriteTime Then Dl = True
Else
Dl = True
End If

If Dl = True Then
Dim LfN As LfDl.Logfile = WbService.GetLogfile(Lf.LogfileName)
File.WriteAllText(Lfloc, LfN.LogfileText)
File.SetLastWriteTime(Lfloc, Lf.LastWriteTime)
Msg &= Lf.LogfileName & vbCrLf
End If

Next

Label1.Text = Msg

Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub

End Class

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''