Use the odata protocol to query windows logs

Source: Internet
Author: User
Tags logentries

Http://www.cnblogs.com/shanyou/archive/2010/03/26/1697316.html

Use the odata protocol to query windows logs

Odata open data protocol is launched by Microsoft for Google's gdata. It aims to promote the standardized open data protocol for Web application database formats. Microsoft defines odata as a protocol based on HTTP, atompub, and JSON, enhance data compatibility between various web applications to provide information access to multiple applications, services, and data stores. In addition, Microsoft has officially launched the odata SDK, including. net, Java, PHP, palm WebOS, and iPhone support. The. NET odata client is based on Apache-authorized open source. Microsoft supports multiple odata products, including Sharepoint Server 2010, Excel 2010, and dynamics.

The first-generation Data Exchange Protocol of Microsoft is ODBC (Open Database Connection Open Database Connectivity), which is still visible in the Development of Windows and Linux native programs, the objective is to provide a unified data interaction API between applications in the operating system, which is functional. Later, Microsoft launched the second generation: ole db, which introduced the Oop-style interactive API and the possibility of cross-Network Data Interaction (through DCOM ), the specific implementation of the ole db standard is a set of C ++ API functions, just like the odbc api in the ODBC standard, the difference is that, ole db APIs comply with COM standards and are object-based (ODBC APIs are simple C APIs ). Using the ole db api, you can write applications that can access any data source that meets the ole db standard, or query processor for a specific data storage) and cursor engine. Therefore, the ole db Standard specifies an application-level protocol between the data user and the provider ). In the era of cloud computing, web applications have become the mainstream. Applications mainly express requirements through HTTP requests and obtain results through HTTP response. ODBC and ole db are no longer available. Microsoft developed its third-generation data interaction protocol: odata open data protocol.

In the SOA world, the most important concept is contract ). In the world of cloud computing, the most important concepts about communication are also contracts. XML has a powerful ability to describe data. Atom format and atompub are both built on XML, which has become a standard under the impetus of Google and Microsoft. However, the real data interaction protocols such as atom/atompub and ODBC/oledb have a fundamental deficiency: the lack of specific descriptions of Data Types reduces the Interaction performance. Lack of control over data queries, such as the range of returned data sets or the paging capability. Microsoft has released odata Based on the EDM model. Here we can also see that the Entity Framework has different strategic considerations for ORM tools such as nhib.pdf.

At the PDC Conference, Microsoft announced a Community Technology Preview (CTP) codenamed "Dallas", an information service built by Windows azure and SQL azure, it allows developers and information workers to use high-quality third-party data sets and content on any platform. "Dallas" can also use the self-help business intelligence and analysis stored data sets of Microsoft technology. The data interaction protocol used by Dallas is odata.

In Microsoft's solutions, WCF is used to process communications between all programs. for data communication, WCF data services is naturally the best choice. First, WCF Data Services is a WCF Service, so you can use all existing WCF knowledge. Secondly, WCF Data Services has implemented the odata topology, so you can focus on the representation of your data format in your program, rather than the real data format transmitted on the network by atompub/JSON. In addition, WCF Data Services focuses on data transmission rather than data storage. Your data can be stored anywhere: local databases, cloud databases, external web services, XML files, and so on. No matter how the data comes from, you can publish/use them in the same way.

Next we will use the WCF data service to publish the windows application logs of the server. Our applications can write logs directly in Windows logs, and then publish logs to other users who need them easily by using the WCF data service. By default, the WCF Data Service uses the Entity Framework. For details about how to use the Entity Framework, see the article WCF Data Service Quickstart. There is also a reflection provider that supports read-only data services. In this example, the reflection provider is used, information see msdn: http://msdn.microsoft.com/en-us/library/dd723653 (vs.100 ). aspx. You can also customize a provider. For more information, see the document "custom data service providers.

First, define a Windows Log entity, similar to datacontract of WCF. Here EDM ing is used:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Data. Services. Common;

Namespace reflectiondataservicedemo
{
[Entitypropertymappingattribute ("Source ",
Syndicationitemproperty. title,
Syndicationtextcontentkind. plaintext, true)]
[Entitypropertymapping ("message ",
Syndicationitemproperty. Summary,
Syndicationtextcontentkind. plaintext, true)]
[Entitypropertymapping ("timegenerated ",
Syndicationitemproperty. Updated,
Syndicationtextcontentkind. plaintext, true)]
[Dataservicekey ("eventid")]
Public class logentry
{
Public long eventid
{
Get;
Set;
}

Public String category
{
Get;
Set;
}

Public String message
{
Get;
Set;
}

Public datetime timegenerated
{
Get;
Set;
}

Public String Source
{
Get;
Set;
}

}

}

The above uses a new feature friendly feeds to map data to the standard Atom element, where dataservicekey is the only required tag, and then uses reflection provider to implement a data source of an iqueryable interface:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. diagnostics;

Namespace reflectiondataservicedemo
{
Public class logdatasource
{
String source;

Public logdatasource (string source)
{
This. Source = source;
}

Public logdatasource ()
{
}

Public iqueryable <logentry> logentries
{
Get {return getentries (). asqueryable (). orderby (E => E. timegenerated );}
}

Private ienumerable <logentry> getentries ()
{
VaR applicationlog = system. Diagnostics. EventLog. geteventlogs (). Where (E => E. log = "application ")
. Firstordefault ();

VaR entries = new list <logentry> ();

If (applicationlog! = NULL)
{
Foreach (eventlogentry entry in applicationlog. Entries)
{
If (Source = NULL | entry. Source. Equals (source, stringcomparison. invariantcultureignorecase ))
{
Entries. Add (New logentry
{
Category = entry. category,
Eventid = entry. instanceid,
Message = entry. Message,
Timegenerated = entry. timegenerated,
Source = entry. source,
});
}
}
}

Return entries. orderbydescending (E => E. timegenerated)
. Take (1, 200 );
}
}

}

Finally, add a WCF data service. The code is very simple. The main task is to publish the above data source through the WCF Data Service:

Using system;
Using system. Collections. Generic;
Using system. Data. Services;
Using system. Data. Services. Common;
Using system. LINQ;
Using system. servicemodel. Web;
Using system. Web;
Using system. configuration;

Namespace reflectiondataservicedemo
{
Public class logdataservice: dataservice <logdatasource>
{
// This method is called only once to initialize service-wide policies.
Public static void initializeservice (dataserviceconfiguration config)
{
// Todo: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
Config. setentitysetaccessrule ("*", entitysetrights. allread );
Config. setserviceoperationaccessrule ("*", serviceoperationrights. All );
Config. dataservicebehavior. maxprotocolversion = dataserviceprotocolversion. V2;
}

Protected override logdatasource createdatasource ()
{
String source = configurationmanager. receivettings ["eventlogsource"];
If (Source = NULL)
{
Throw new applicationexception ("The eventlogsource logging etting is missing in the configuration file ");
}

Return new logdatasource (source );

}
}
}

Let's write a simple console client to consume this service, and use the add service reference of Visual Studio to generate the client code of the service. You can also use a plug-in.Open Data Protocol visualizerView the odata data returned by the service. For more information about how to obtain and install this tool, see the vs2010 extension. View in digoal referenced by the Service.

The client code is also very simple;

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace clientdemo
{
Class Program
{
Static void main (string [] ARGs)
{
Logdataservice. logdatasource logsource = new logdataservice. logdatasource (New uri ("http: // localhost: 3399/logdataservice. svc /"));
Foreach (VAR item in logsource. logentries)
{
Console. writeline (string. Format ("from {0} event {1}, content {2}", item. Source, item. eventid, item. Message ));
}
Console. Read ();
}
}
}

Project code can get http://cid-33478a966734670f.skydrive.live.com/self.aspx/.Public/ReflectionDataServiceDemo.zip here


Author: freedom, innovation, research, exploration ......
Source: http://shanyou.cnblogs.com/
Copyright: The copyright of this article is shared by the author and the blog
Reprinted: you are welcome to reprinted. To save the author's Creative Enthusiasm, please [reprinted] As required. Thank you.
Requirement: This statement must be retained without the consent of the author; the original text connection must be provided in the article; otherwise, the legal liability must be investigated.

Odata open data protocol is launched by Microsoft for Google's gdata. It aims to promote the standardized open data protocol for Web application database formats. Microsoft defines odata as a protocol based on HTTP, atompub, and JSON, enhance data compatibility between various web applications to provide information access to multiple applications, services, and data stores. In addition, Microsoft has officially launched the odata SDK, including. net, Java, PHP, palm WebOS, and iPhone support. The. NET odata client is based on Apache-authorized open source. Microsoft supports multiple odata products, including Sharepoint Server 2010, Excel 2010, and dynamics.

The first-generation Data Exchange Protocol of Microsoft is ODBC (Open Database Connection Open Database Connectivity), which is still visible in the Development of Windows and Linux native programs, the objective is to provide a unified data interaction API between applications in the operating system, which is functional. Later, Microsoft launched the second generation: ole db, which introduced the Oop-style interactive API and the possibility of cross-Network Data Interaction (through DCOM ), the specific implementation of the ole db standard is a set of C ++ API functions, just like the odbc api in the ODBC standard, the difference is that, ole db APIs comply with COM standards and are object-based (ODBC APIs are simple C APIs ). Using the ole db api, you can write applications that can access any data source that meets the ole db standard, or query processor for a specific data storage) and cursor engine. Therefore, the ole db Standard specifies an application-level protocol between the data user and the provider ). In the era of cloud computing, web applications have become the mainstream. Applications mainly express requirements through HTTP requests and obtain results through HTTP response. ODBC and ole db are no longer available. Microsoft developed its third-generation data interaction protocol: odata open data protocol.

In the SOA world, the most important concept is contract ). In the world of cloud computing, the most important concepts about communication are also contracts. XML has a powerful ability to describe data. Atom format and atompub are both built on XML, which has become a standard under the impetus of Google and Microsoft. However, the real data interaction protocols such as atom/atompub and ODBC/oledb have a fundamental deficiency: the lack of specific descriptions of Data Types reduces the Interaction performance. Lack of control over data queries, such as the range of returned data sets or the paging capability. Microsoft has released odata Based on the EDM model. Here we can also see that the Entity Framework has different strategic considerations for ORM tools such as nhib.pdf.

At the PDC Conference, Microsoft announced a Community Technology Preview (CTP) codenamed "Dallas", an information service built by Windows azure and SQL azure, it allows developers and information workers to use high-quality third-party data sets and content on any platform. "Dallas" can also use the self-help business intelligence and analysis stored data sets of Microsoft technology. The data interaction protocol used by Dallas is odata.

In Microsoft's solutions, WCF is used to process communications between all programs. for data communication, WCF data services is naturally the best choice. First, WCF Data Services is a WCF Service, so you can use all existing WCF knowledge. Secondly, WCF Data Services has implemented the odata topology, so you can focus on the representation of your data format in your program, rather than the real data format transmitted on the network by atompub/JSON. In addition, WCF Data Services focuses on data transmission rather than data storage. Your data can be stored anywhere: local databases, cloud databases, external web services, XML files, and so on. No matter how the data comes from, you can publish/use them in the same way.

Next we will use the WCF data service to publish the windows application logs of the server. Our applications can write logs directly in Windows logs, and then publish logs to other users who need them easily by using the WCF data service. By default, the WCF Data Service uses the Entity Framework. For details about how to use the Entity Framework, see the article WCF Data Service Quickstart. There is also a reflection provider that supports read-only data services. In this example, the reflection provider is used, information see msdn: http://msdn.microsoft.com/en-us/library/dd723653 (vs.100 ). aspx. You can also customize a provider. For more information, see the document "custom data service providers.

First, define a Windows Log entity, similar to datacontract of WCF. Here EDM ing is used:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Data. Services. Common;

Namespace reflectiondataservicedemo
{
[Entitypropertymappingattribute ("Source ",
Syndicationitemproperty. title,
Syndicationtextcontentkind. plaintext, true)]
[Entitypropertymapping ("message ",
Syndicationitemproperty. Summary,
Syndicationtextcontentkind. plaintext, true)]
[Entitypropertymapping ("timegenerated ",
Syndicationitemproperty. Updated,
Syndicationtextcontentkind. plaintext, true)]
[Dataservicekey ("eventid")]
Public class logentry
{
Public long eventid
{
Get;
Set;
}

Public String category
{
Get;
Set;
}

Public String message
{
Get;
Set;
}

Public datetime timegenerated
{
Get;
Set;
}

Public String Source
{
Get;
Set;
}

}

}

The above uses a new feature friendly feeds to map data to the standard Atom element, where dataservicekey is the only required tag, and then uses reflection provider to implement a data source of an iqueryable interface:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. diagnostics;

Namespace reflectiondataservicedemo
{
Public class logdatasource
{
String source;

Public logdatasource (string source)
{
This. Source = source;
}

Public logdatasource ()
{
}

Public iqueryable <logentry> logentries
{
Get {return getentries (). asqueryable (). orderby (E => E. timegenerated );}
}

Private ienumerable <logentry> getentries ()
{
VaR applicationlog = system. Diagnostics. EventLog. geteventlogs (). Where (E => E. log = "application ")
. Firstordefault ();

VaR entries = new list <logentry> ();

If (applicationlog! = NULL)
{
Foreach (eventlogentry entry in applicationlog. Entries)
{
If (Source = NULL | entry. Source. Equals (source, stringcomparison. invariantcultureignorecase ))
{
Entries. Add (New logentry
{
Category = entry. category,
Eventid = entry. instanceid,
Message = entry. Message,
Timegenerated = entry. timegenerated,
Source = entry. source,
});
}
}
}

Return entries. orderbydescending (E => E. timegenerated)
. Take (1, 200 );
}
}

}

Finally, add a WCF data service. The code is very simple. The main task is to publish the above data source through the WCF Data Service:

Using system;
Using system. Collections. Generic;
Using system. Data. Services;
Using system. Data. Services. Common;
Using system. LINQ;
Using system. servicemodel. Web;
Using system. Web;
Using system. configuration;

Namespace reflectiondataservicedemo
{
Public class logdataservice: dataservice <logdatasource>
{
// This method is called only once to initialize service-wide policies.
Public static void initializeservice (dataserviceconfiguration config)
{
// Todo: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
Config. setentitysetaccessrule ("*", entitysetrights. allread );
Config. setserviceoperationaccessrule ("*", serviceoperationrights. All );
Config. dataservicebehavior. maxprotocolversion = dataserviceprotocolversion. V2;
}

Protected override logdatasource createdatasource ()
{
String source = configurationmanager. receivettings ["eventlogsource"];
If (Source = NULL)
{
Throw new applicationexception ("The eventlogsource logging etting is missing in the configuration file ");
}

Return new logdatasource (source );

}
}
}

Let's write a simple console client to consume this service, and use the add service reference of Visual Studio to generate the client code of the service. You can also use a plug-in.Open Data Protocol visualizerView the odata data returned by the service. For more information about how to obtain and install this tool, see the vs2010 extension. View in digoal referenced by the Service.

The client code is also very simple;

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace clientdemo
{
Class Program
{
Static void main (string [] ARGs)
{
Logdataservice. logdatasource logsource = new logdataservice. logdatasource (New uri ("http: // localhost: 3399/logdataservice. svc /"));
Foreach (VAR item in logsource. logentries)
{
Console. writeline (string. Format ("from {0} event {1}, content {2}", item. Source, item. eventid, item. Message ));
}
Console. Read ();
}
}
}

Project code can get http://cid-33478a966734670f.skydrive.live.com/self.aspx/.Public/ReflectionDataServiceDemo.zip here

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.